Solved

PHPMailer: attached PDF embedded in email body

Posted on 2010-09-20
6
2,046 Views
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 comcast.net 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

0
Comment
Question by:febone
6 Comments
 
LVL 4

Accepted Solution

by:
Parag_t567 earned 125 total points
ID: 33722297
0
 
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.
0
 
LVL 48

Expert Comment

by:hernst42
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:Progressed
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):

      <?php
      include("mailclass.php");
      
          //-- 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', 'mail@reciever.com', 'Sender name', 'sender@mailadress.com');
      
      // $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. */



error_reporting(E_ALL);



class MailAttach

{

//-----------------------

//-- constructor

//-----------------------

function MailAttach()

{

//-----------------------

//-- config (change this)

//-----------------------



$this->go_email = "emailadres@ontvanger.nl"; // to e-mail address

$this->go_name = "Naam van Ontvanger"; // to name



$this->reply_email = "emailadres@afzender.nl"; // 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))

die($this->error[4]);

else

$attachment_name[] = $filename;



//-- read content

if ($fp = @fopen($filename, 'r'))

{

if ($filesize = @filesize($filename))

$inhoud = fread($fp, $filesize);

else

die($this->error[1]);



fclose($fp);

}

else

die($this->error[0]);



//-- check if filesize is below maximum

if ($filesize > ($this->mb * $this->max_file_size))

die($this->error[3]);



//-- check for filesize counting

if ($this->file_size_count == 1)

{

//-- calculate total of all files

if (empty($total_filesize))

$total_filesize = $filesize;

else

{

$total_filesize = $total_filesize + $filesize;



//-- check if total filesize is below maximum

if ($total_filesize > ($this->mb * $this->max_file_size))

die($this->error[3]);

}

}



//-- 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];

else

die($this->error[2]);

}

//-- no attachment has been added, send e-mail without attachment

else

{

$headers = "From: " . $from_name . " <" . $from_email . ">\r\n";

$headers .= "MIME-Version: 1.0\r\n";

$message;



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



return $this->result[1];

}

}

}

?>

Open in new window

0
 

Author Comment

by:febone
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 cox.net address from phpmailer

From: FFF Technical Services <tech_services@fashionfitformula.com>
Message-ID: <b0cfb5906fece7d6fc673eb2a8aae4bb@www.fashionfitformula.com>
X-Priority: 3 X-Mailer: PHPMailer (phpmailer.codeworxtech.com) [version 2.3]
MIME-Version: 1.0 Content-Type: multipart/mixed;
boundary="b1_b0cfb5906fece7d6fc673eb2a8aae4bb"



--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 corporate@fashionfitformula.com 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
Formula=0D

(410) 330-8432 Direct=0D

kmcfadden@fashionfitformula.com=0D

www.fashionfitformula.com=0D

WBENC Certified=0D

=0D

=0D

--b1_b0cfb5906fece7d6fc673eb2a8aae4bb Content-Type:
application/octet-stream; name="sharon_email_1553.pdf"
Content-Transfer-Encoding: base64 Content-Disposition: attachment;
filename="sharon_email_1553.pdf"
JVBERi0xLjMKJeLjz9MKCjEgMCBvYmoKPDwgL1R5cGUgL0NhdGFsb2cKL091dGxpbmVzIDIgMCBS
Ci9QYWdlcyAzIDAgUgovT3BlbkFjdGlvbiA4IDAgUiA+PgplbmRvYmoKMiAwIG9iago8PCAvVHlw
0
 

Author Closing Comment

by:febone
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now