tags:

views:

267

answers:

3

I have been struggling with trying to send an email with an attachment using PHP. It used to work but the message body was scrambled. Now I have got the message body to work but the attachment corrupts. I used to use base64 encoding for the message body but now use 7bit. Can anyone tell me what I am doing wrong?

PS please do not tell me that I should be using a pre-made class to do this. I have tried several and they have all failed to work. If I do not overcome these problems I will never learn how to do it properly. Thanks

//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Your Disneyland Paris entry';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \n
$mime_boundary = "<<<--==+X[".md5(time())."]";

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
$fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));


$headers .= "From: [email protected] <[email protected]>"."\n";

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

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

$message .= "\n";
$message .= "--".$mime_boundary."\n";

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n";
$message .= "\n";
$message .= "messagebody \n";
$message .= "--".$mime_boundary."" . "\n";

$message .= "Content-Type: application/octet-stream;\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\n";
$message .= "Content-Transfer-Encoding: 7bit \n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"CTF_brochure.pdf\"\n";
$message .= "\n";
$message .= $fileContent;
$message .= "\n";
$message .= "--".$mime_boundary."--\n";

//send the email
$mail_sent = mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
+5  A: 

I could be wrong but I believe you will have to encode the PDF somehow, 7bit won't work as the PDF file will have content outside the range. Why not use base64 for the PDF?

Josh
thanks. I have just given it a go and no change.
Drew
Which code specifically did you change? Did you try base64 encoding the entire message or just the attachment? What did the MIME headers look like after you made the change?
Josh
I just made the change to the attachment. If i base64 the entire message I get strange characters as the message body
Drew
So you did `Content-Transfer-Encoding: base64 \n` before outputting the attachment? If so, I'm not sure what went wrong... It looks like everything else is right. Could you post your complete, new code so the community could test it?
Josh
A: 

I would suggest looking at phpmailer if you want to do complex email.

Craig
+1  A: 

I know you've said about pre-built classes but there is a reason that people do this - why re-invent the wheel? I use SwiftMailer for projects - it couldn't be simpler. See this SwiftMailer example for 13 lines (including some blank ones) of how to create a message, add an attachment and send.

As to the resolution of your actual query, upvote to Josh's answer - I'd second changing the encoding and seeing how you get on. Have you tried getting an example email message which has an attachment that works, and examining the raw data?

richsage
i did try swiftmailer for this but it refused to work. the server it's set up on is very twitchy
Drew