Send email with pdf attachment using php

Hi, can anyone assist. I'm trying to send an email with a pdf attachment using php. What would be the best approach as I have tried using php mail but I keep getting the following message - Warning: filesize(): stat failed...

Who is Participating?
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
@logudotcom, you might want to start using the code from the link I provided.  It is the most current version and would be using 'preg_replace' instead of "eregi_replace" to work the newest versions of PHP.   'eregi_replace' is deprecated as of PHP 5.3 and will cause a warning.
Maybe this will assist

// download fpdf class (
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see
$pdf->Cell(40,10, "this is a pdf example");
// email stuff (change data below)
$to = ""; 
$from = ""; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);

Open in new window

Dave BaldwinFixer of ProblemsCommented:
Loganathan NatarajanLAMP DeveloperCommented:
Better try phpMailer class to send attached email, ref.



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

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

$mail->AddReplyTo("","First Last");

$mail->SetFrom('', 'First Last');

$mail->AddReplyTo("","First Last");

$address = "";
$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->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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


Open in new window

Ray PaseurCommented:
Can you please give us a little more information about the exact issues?  PHP filesize() would appear to be related not to the mail process, but to file creation.  So a warning from that function would tell me to look elsewhere for the issues.

A tangential thought... Why send a PDF at all?  If it's possible to send a link to a PDf instead, your bandwidth requirements will be much smaller and it won't clutter the client inbox.  They can download a PDF with a single click if they want to receive it and store it.

Best regards, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.