PHPMailer: attached PDF embedded in email body

Posted on 2010-09-20
Last Modified: 2012-05-10
This application attaches a pdf file and mails it. The email with attachments came through fine in yahoo, gmail and a POP server, but was corrupt when sent to a address.

In the comcast case the pdf was inserted in the body of the email as code preceded by

Content-Type: application/octet-stream; name="a_email_1555.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="a_email_1555.pdf"

<mass of letters/numbers>

Am I doing something wrong or is this a comcast issue? Thanks.

$mail = new PHPMailer();
$mail->Mailer = self::FFF_USE_MAIL_SYSTEM;
$mail->Host = self::FFF_EMAIL_HOST;
$mail->From = self::FFF_EMAIL_FROM;
$mail->FromName = self::FFF_EMAIL_FROM_NAME;
$mail->WordWrap = 50;

$attach =  self::PDF_FILE_DIR.$this->getPDF_File_Name($arrPDF);

(getPDF_File_Name just returns a filename to look for in PDF_FILE_DIR )

$mail->AddAttachment($attach, 'results.pdf'); // results pdf

Then add addresses and send.

Open in new window

Question by:febone
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Parag_t567 earned 125 total points
ID: 33722297
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33722311
You may try another simple email with attachment of pdf to comcast and check whether it allows to read pdf or not.
LVL 48

Expert Comment

ID: 33723258
You might add a little body to the mail and check again. PHPMailes does encode the mails properly so in this case it might be a commcast issue.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Assisted Solution

Progressed earned 125 total points
ID: 33723406
Hi, maybe you can try this... I use this mailerclass and it works perfectly for all attachments...

See code snippet for mailerclass and heres the code (it could be that i made a error in commenting some lines):

          //-- subject of e-mail
    $subject = "subjectline";
    //-- message of e-mail
    $message = "this is your mailcontent";
      // uncomment the foreach loop if you want to attach attachments when file is uploaded trough a form.
      // What it does: upload file to cache dir. attach every uploaded file to e-mail. empty cache dir.
      // if (!empty($_FILES)) {
      //      $uploads_dir = "./cache";

      //            foreach ($_FILES["FileData"]["tmp_name"] as $key => $error) {

      //                        $tmp_name = $_FILES["FileData"]["tmp_name"][$key];
      //                        $name = $_FILES["FileData"]["name"][$key];
      //                        move_uploaded_file($tmp_name, "$uploads_dir/$name");
      //                        $catchfile = $uploads_dir . '/' . $name;

      //                        $files[] = $catchfile;
      // }

      // comment the beneath line if you use the foreachloop
      $files[] = 'yourfile.pdf';

    //-- open class
    $mail = new MailAttach();
    echo $mail->SendMailAttach($subject, $message, $files, 'name reciever', '', 'Sender name', '');
      // $dir = 'cache/';
      // foreach(glob($dir.'*.*') as $v)
      // {
    // unlink($v);
      // }
      // }
new file to include: mailclass.php

/* Content Management System */
/* -------------------------------------------------------------*/
/* */
/* File : class_mail_attachment.php */
/* Version : 1.0.1 */
/* Last modified : 20/05/2006 */
/* */
/* Author : B.Kreleger */
/* */

/* Description */
/* Mail class which can send optional attachments. */


class MailAttach
//-- constructor
function MailAttach()
//-- config (change this)

$this->go_email = ""; // to e-mail address
$this->go_name = "Naam van Ontvanger"; // to name

$this->reply_email = ""; // from e-mail address
$this->reply_name = "Naam van Afzender"; // from name

//-- attachment settings
$this->mb = 1048576; // 1Mb in bytes
$this->max_file_size = 5; // Max file size of attachment(s) in Mb's

$this->file_size_count = 1; // 1 = total size of all attachments
// 2 = size per attachment

//-- errors

//-- file error
$this->error[0] = "<pre><code><b>Error 0:</b><br />Mail could not be send: File could not be opened or does not exist.</code></pre>";

//-- file error
$this->error[1] = "<pre><code><b>Error 1:</b><br />Mail could not be send: Filesize is unkown.</code></pre>";

//-- sendmail error
$this->error[2] = "<pre><code><b>Error 2:</b><br />Mail could not be send: Something's wrong with the mail function.</code></pre>";

//-- attachment error
$this->error[3] = "<pre><code><b>Error 3:</b><br />Mail could not be send: Attachment is too big (max: " . $this->mb * $this->max_file_size . " bytes).</code></pre>";

//-- attachment error
$this->error[4] = "<pre><code><b>Error 4:</b><br />Mail could not be send: Attachment is not unique.</code></pre>";

//-- results

//-- email with attachment sent
$this->result[0] = "<p><b>E-mail met bijlage verstuurd</b></p>";

//-- email without attachment sent
$this->result[1] = "<p><b>E-mail verstuurd</b></p>";

//-- sendmail function
function SendMailAttach ($subject, $message, $files = '', $to_name = '', $to_email = '', $from_name = '', $from_email = '')
//-- generate boundary
$bound = md5(uniqid(time()));

//-- check for "TO" addresses
if (empty($to_name))
$to_name = $this->go_name;
if (empty($to_email))
$to_email = $this->go_email;

//-- check for "FROM" adresses
if (empty($from_name))
$from_name = $this->reply_name;
if (empty($from_email))
$from_email = $this->reply_email;

if (!empty($files))
$headers = "From: " . $from_name . " <" . $from_email . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";

//-- mixed
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $bound . "\"\r\n";

//-- sent attachment
$headers .= "Content-Disposition: attachment\r\n";

//-- create body
$body = "This is a multi-part message in MIME format.\r\n";
$body .= "\r\n";

//-- bound
$body.= "--" . $bound . "\r\n";

//-- charset
$body.= "Content-Type: text/plain; charset=iso-8859-1\r\n";

//-- 7bit coding
$body.= "Content-Transfer-Encoding: 7bit\r\n";
$body.= "\r\n";

//-- message
$body.= $message ."\r\n";

foreach($files as $filename)
//-- set array with filename
if (empty($attachment_name))
$attachment_name = array();

if (in_array($filename, $attachment_name))
$attachment_name[] = $filename;

//-- read content
if ($fp = @fopen($filename, 'r'))
if ($filesize = @filesize($filename))
$inhoud = fread($fp, $filesize);


//-- check if filesize is below maximum
if ($filesize > ($this->mb * $this->max_file_size))

//-- check for filesize counting
if ($this->file_size_count == 1)
//-- calculate total of all files
if (empty($total_filesize))
$total_filesize = $filesize;
$total_filesize = $total_filesize + $filesize;

//-- check if total filesize is below maximum
if ($total_filesize > ($this->mb * $this->max_file_size))

//-- bound
$body .= "--" . $bound . "\r\n";

//-- userfriendly filename
$show_filename_array = explode("/", $filename);
$show_filename = $show_filename_array[count($show_filename_array)-1];

//-- content type + name file
$body .= "Content-Type: application/octet-stream; name=" . $show_filename . ";\r\n";

//-- base64 encoding
$body .= "Content-Transfer-Encoding: base64\r\n";

//-- attached as attachment
$body .= "Content-disposition: attachment\r\n";
$body .= "\n";

//-- content of attachment
$body .= chunk_split(base64_encode($inhoud)) . "\r\n";

if (mail($to_name . " <" . $to_email . ">", $subject, $body, $headers))
return $this->result[0];
//-- no attachment has been added, send e-mail without attachment
$headers = "From: " . $from_name . " <" . $from_email . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";

mail($to_name . " <" . $to_email . ">", $subject, $message, $headers);

return $this->result[1];

Open in new window


Author Comment

ID: 33726118
Thanks for comments so far; will get around to working with them today. In case it helps anybody see what is going on another email came back looking like this - sent to a address from phpmailer

From: FFF Technical Services <>
Message-ID: <>
X-Priority: 3 X-Mailer: PHPMailer ( [version 2.3]
MIME-Version: 1.0 Content-Type: multipart/mixed;

--b1_b0cfb5906fece7d6fc673eb2a8aae4bb Content-Type: text/plain; charset =
"iso-8859-1" Content-Transfer-Encoding: quoted-printable =0D Dear Sharon
,=0D =0D You are on your way to looking fabulous in=0D everything you wear.
We are emailing you the=0D results of your measurements and an
informational=0D brochure. So don't delay, start having fun with=0D the
clothes in your closet!=0D =0D Please note, if you were measured on the
right and=0D left side of your body because there was a=0D difference from
one side to the other, you will=0D need to use the measurements from the
floor up=0D starting with the mid-calf point to the waist=0D point. We are
taking the averages between the two=0D side to put your body in balance.=0D
=0D We love to hear from our clients on how the=0D Fashion Fit Formula has
helped you. Email me at=0D and please tell
a=0D friend about us.=0D =0D Thanks,=0D Kathy.=0D =0D Janet Wood, CEO=0D =0D
Kathy J. McFadden=0D President=0D Pivotal Edge, Inc.=0D Fashion Fit

(410) 330-8432 Direct=0D

WBENC Certified=0D



--b1_b0cfb5906fece7d6fc673eb2a8aae4bb Content-Type:
application/octet-stream; name="sharon_email_1553.pdf"
Content-Transfer-Encoding: base64 Content-Disposition: attachment;

Author Closing Comment

ID: 33756338
In the end I went with inserting links into the body of the email pointing to the documents I wanted to attach, rather than attaching them to the email. Less hassle that way and more options for the recipient.

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question