I've run into a little problem with sending email in PHP. I've been creating the body of the message using ob_get_clean() and it works just fine for me. However, it was asked of me why I wasn't just writing the message directly to the variable. I thought I'd give it a try and I'm getting a weird result.
Using the ob_get method, my email creates and sends just fine (multi-part with attachments), but if I try to write the body directly into the variable, the email message that comes through appears as plain text (it sends the raw information without actually formatting or anything).
Here's a sample message I test. It sends just fine and comes across as it should:
PHP Code:
<?PHP
$random_hash=md5(date('r',time()));
$to="you@your.domain";
$subject="Test message.";
$headers="From: you@your.domain\r\nReply-To: you@your.domain";
$headers.="\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//Message collected as output buffer
ob_start();
?>
--PHP-mixed-<?PHP echo $random_hash;?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?PHP echo $random_hash;?>"
--PHP-alt-<?PHP echo $random_hash;?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This is a simple text message.
--PHP-alt-<?PHP echo $random_hash;?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><body><div style="text-align:center;">This is a complex HTML message.</div></body></html>
--PHP-alt-<?PHP echo $random_hash;?>--
--PHP-mixed-<?PHP echo $random_hash;?>--
<?PHP
$message=ob_get_clean();
//End of message
$mail_sent=@mail($to,$subject,$message,$headers);
echo $mail_sent ? "Message sent." : "Great, you broke it.";
?>
And here's the same message, but with the $message written as a variable directly rather than pulled from the ob:
PHP Code:
<?PHP
$random_hash=md5(date('r',time()));
$to="you@your.domain";
$subject="Test message.";
$headers="From: you@your.domain\r\nReply-To: you@your.domain";
$headers.="\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//Message created as variable
$message="--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
This is a simple text message.
--PHP-alt-$random_hash
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<html><body><div style=\"text-align:center;\">This is a complex HTML message.</div></body></html>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash--";
//End of message
$mail_sent=@mail($to,$subject,$message,$headers);
echo $mail_sent ? "Message sent." : "Great, you broke it.";
?>
If you run them, you'll see what I mean by raw message.
Could someone tell me what I'm doing wrong in the latter file?
***EDIT***
Checking the two $message variables, I found they are both string-type and, when echoed, APPEAR identical, but strlen of the $message from the broken file is two characters longer than the first (working) one. I don't know where those two characters are coming from. As I said, they APPEAR identical when echoed...
Bookmarks