mail() and pdf attachment

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am trying to send a pdf file as an attachment with the mail()
function. Unfortunately, the pdf file does not show up as an
attachment. I get its coding as text along with this:

MIME-Version: 1.0
Content-Type: multipart/mixed;

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

this is the message

Content-Type: application/pdf;
 name="Test file for jmail.pdf"
Content-Disposition: attachment;
 filename="Test file for jmail.pdf"
Content-Transfer-Encoding: base64


... a lot omitted



Can anyone tell me what is wrong?


Jan Nordgreen

Re: mail() and pdf attachment

Quoted text here. Click to load it

Uhmm I'm not sure, It's hard when you can't see the codes thats send
the email.
But here is PHP code to send a email with file attachment.

It's taken from my tutorial on

 $to_email             = "";
     $subject              = "A file from my website";
     $filename             = $_SERVER["DOCUMENT_ROOT"] . "/files/";
     $filetype             = "application/octet-stream";
     $options              = "From:";
     $text                 = "Hi John\n\nHere is the zip file i told
you about :-)";

     $semi_rand = md5(time());
     $mime_boundary = "==Multipart_Boundary_xx";

    //Read the file
     $handle         = fopen($filename, 'rb');
     $filecontents   = fread($handle, filesize($filename));

     //base64 encode the file
     $filecontents   = @base64_encode($filecontents);

     //Write the mime message
     $options            .= "\nMIME-Version: 1.0";
     $options            .= "\nContent-Type: multipart/mixed;\n
boundary=\"". $mime_boundary ."\"\n\n";

     $content            .= "This is a multi-part message in MIME
     $content            .= "--$mime_boundary\n";

     // Adding the text
     $content          .= "Content-Type: text/html;
     $content          .= "Content-Transfer-Encoding: 8bit\n\n";
     $content          .= $text . "\n\n";

     // Adding the file
     $content          .= "--". $mime_boundary . "\n";
     $content          .= "Content-Type: application/octet-stream;
name=\"" . basename($filename) . "\"\n";
     $content          .= "Content-Transfer-Encoding: base64\n\n";
     $content          .= chunk_split($filecontents) . "\n\n";
     $content          .= "--". $mime_boundary . "\n";

     //Send the mail

I hope it was of some help to you


Re: mail() and pdf attachment

Hi Michael,

Thank you for answering!

     $fromname = $_SESSION['myname'];
     $fromaddress = $_SESSION['myemailaddress'];
     $headers = "From: \"".$fromname."\" <".$fromaddress.">\n";
     $bcc = $_SESSION['myemailaddress'];
     $headers .= 'Bcc: '.$bcc. "\n";
     $headers .= "\nMIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"\"";

This was my offending code!

I had too many line shifts (\n) in $headers.

I changed the last line to:

     $headers .= "MIME-Version: 1.0\n" .
                    "Content-Type: multipart/mixed;\n" .
                    " boundary=\"\"";

and then it worked!


Jan Nordgreen

Re: mail() and pdf attachment

Quoted text here. Click to load it

Ahh that explains a lot.
I'm glad to hear you got it to work ;-)


Site Timeline