14

I am sending an email using PHP mail function, but I would like to add a specified PDF file as a file attachment to the email. How would I do that?

Here is my current code:

$to = "me@myemail.com";
$subject = "My message subject";
$message = "Hello,\n\nThis is sending a text only email, but I would like to add a PDF attachment if possible.";
$from = "Jane Doe <janedoe@myemail.com>";

$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);

echo "Mail Sent!";
MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • [Please follow this link to generate dynamic PDF and send mail ][1] [1]: http://stackoverflow.com/questions/18396714/how-to-send-email-with-pdf-attachment-using-php/22141096#22141096 – Dheeraj singh Mar 03 '14 at 14:59

4 Answers4

20

You should consider using a PHP mail library such as PHPMailer which would make the procedure to send mail much simpler and better.

Here's an example of how to use PHPMailer, it's really simple!

<?php

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

An alternative to PHPMailer is http://swiftmailer.org/

Alastair
  • 6,837
  • 4
  • 35
  • 29
josmith
  • 1,049
  • 7
  • 17
  • what if my attachment is stored on a remote server? does anything changes or it should work ? – paulalexandru Aug 14 '14 at 12:25
  • [AddAttachment()](https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L2230) does not support remote files: `Add an attachment from a path on the filesystem` Use *file_get_contents()* in advance, @paulalexandru ...? :/ – Alastair Aug 25 '15 at 07:53
  • 8
    You have not answered the question. You have merely presented an alernative solution. The user required the PHP mail function to send a pdf. – Nebula Nov 13 '15 at 09:04
  • 1
    + @Nebula I do not know how this is the accepted answer. – messinismarios Jan 29 '20 at 15:26
3

Simple answer: Don't do it. building a MIME email by hand is a painful business, and VERY easy to screw up.

Instead, use PHPMailer or Swiftmailer. It's almost trivial to do attachments with them, and you get FAR FAR FAR better feedback in case something does blow up, v.s. the simple true/false that mail() condescends to spit out.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

To eliminate deprecation errors,

Replace

$body             = eregi_replace("[\]",'',$body);

With

$body             = preg_replace('/\.([^\.]*$)/i','',$body);
dippas
  • 58,591
  • 15
  • 114
  • 126
sspence65
  • 125
  • 1
  • 6
0

This code is 100% Working The file here is in my remote server and for sending this remote file I've used this $mail->addStringAttachment(file_get_contents($path), 'brochure.pdf'); and it has worked for me. The first parameter $path here defines your file path where your file is stored and Second parameter brochure.pdf defines your file name to be in email. Hope it helps.

<?php

$html="Hi";
$path = "your_file_path.pdf";

include('smtp/PHPMailerAutoload.php');
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host="smtp.gmail.com";
$mail->Port=587;
$mail->SMTPSecure="tls";
$mail->SMTPAuth=true;
$mail->Username="YOUR GMAIL USERNAME";
$mail->Password="YOUR GMAIL APP PASSWORD";
$mail->SetFrom("ADD FROM MAIL");
$mail->addAddress("ADD SEND TO");
$mail->IsHTML(true);
$mail->Subject="Sending mail with attached PDF";
$mail->Body=$html;
$mail->addStringAttachment(file_get_contents($path), 'brochure.pdf');
$mail->SMTPOptions=array('ssl'=>array(
    'verify_peer'=>false,
    'verify_peer_name'=>false,
    'allow_self_signed'=>false
    ));

if ($mail->send()) {
     echo "done";
} else {
    // $form_message = 'Message Delivery Failed, Please send again!';
}

?>

Remo
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '22 at 07:30
  • Welcome to SO! Please don't post code-only answers but add a little textual explanation about how and why your approach works and what makes it different from the other answers given. You can find out more at our ["How to write a good answer"](https://stackoverflow.com/help/how-to-answer) page. – ahuemmer Dec 16 '22 at 07:47