tags:

views:

79

answers:

1

I have been having an ongoing fight with this email setup. The code below works on my server but on the client's server it displays the message source (from reply to) as the message body. The server it is on is a plesk server. Can anyone help me with this? I am sure it is a problem with with the header because if I take out the reply to header it displays correctly in hotmail and googlemail, but not regular email accounts. Thanks in advance for your help.

UPDATE

It displays fine in my email account using thunderbird but my boss, using macmail, still gets nothing but lines of source code. we both use the same server for our emails.

SECOND UPDATE

It now works in everything i have access to except for macmail. The file simply won't open, saying it's corrupt or not recognized. Any ideas?

//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Email with Attachment';
//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 \r\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]>"."\r\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.\r\n";

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

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "\r\n";
$message .= "Email content and what not: \r\n";
$message .= "This is the file you asked for! \r\n";
$message .= "--".$mime_boundary."" . "\r\n";

$message .= "Content-Type: application/octet-stream;\r\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\r\n";
$message .= "Content-Transfer-Encoding: base64 \r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"CTF_brochure.pdf\"\r\n";
$message .= "\r\n";
$message .= $fileContent;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\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";
+2  A: 

They probably have a mail server that expects "\n" as the line ending. Horrible, but it happens!

You're also missing the "--" from the final MIME boundary:

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

//send the email
$mail_sent = mail($to, $subject, $message, $headers);

Usual note about using a library instead of writing you own...

Greg
thanks for this, I do appreciate it. i now get nothing displayed at all. I have tried several libraries and have not been successful in getting them to work on this and from my own learning it's always better for me to be able to get it done myself.
Drew
Could you post the message source that is generated (snipping most of the PDF out)
Greg
Content-Type: text/plain; charset="iso-8859-1"Content-Transfer-Encoding: 7bitEmail content and what not: This is the file you asked for! Content-Type: application/octet-stream; name="CTF-brochure.pdf"Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="CTF_brochure.pdf"------The rest is just hash string
Drew
just tried your fix again and it now works. mostly! i get an email with the correct message and an attachment but the file doesn't open! thanks again
Drew