what's wrong with my Content-Type:text/plain
Posted: Mon May 11, 2009 3:03 pm
I'm trying to send an e-mail with an attachment. The e-mail arrives with the correct attachment but no text in the e-mail body. What am I doing wrong? The body text is "This part of the message should be in the e-mail body".
Code: Select all
<?php
$mail_to = "admin@domain.com";
$mail_subject = "Here is your attachment";
$mail_header = 'From: John Tester <john@email.com>'. "\r\n" . 'Reply-To: John Tester <john@email.com>' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$file_name = "doc1.doc";
$file_path = "docs"."/".$file_name;
$file_type = "application/msword";
$attmt = fopen($file_path,"rb");
$data = fread($attmt, filesize($file_path));
fclose($attmt);
//attachment MIME format added to header info
$semi_rand = md5(time());
$mime_boundary = "==$semi_rand==";
$mail_header.= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"" . "Content-Transfer-Encoding: 7bit\n\n";
//body of the e-mail address
$mail_msg = "--{$mime_boundary}\n" . "Content-Type:text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . "[color=#FF0000]Hello. This part of the message should be in the e-mail body[/color].\n\n";
$data = chunk_split(base64_encode($data));
$mail_msg .= "--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
"Content-Disposition: attachment; \n".
"Filename:\"{$file_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" .
//The last boundary marker (defining the end of the MIME e-mail) must start and stop with two hyphens.
"--{$mime_boundary}--\n";
if(mail($mail_to, $mail_subject, $mail_msg, $mail_header)){
echo "Message was sent OK. " . time();
}
else {
echo "Message was not sent.";
}
?>