MIME email misbehaving

I recently changed something in an old script of mine and now it doesn’t work properly anymore. I don’t know what I did, and I don’t have a backup, oops!

The code below is supposed to take inputs from a web form and turn them into an e-postcard, attaching and sending it as a jpeg on an email, ideally to be displayed inline.

[php]$name = $_POST[‘sendername’];
$to = $_POST[‘recipient’];
$from = $_POST[‘senderemail’];
$subject = basename( $name)." has sent you a jCard!";
$message = $_POST[‘text’];
$bcc = $_POST[‘bcc’];
$finalimage = $_POST[‘finalimage’];

//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 rn
$headers = “From: “.$name.” <”.$from.">\r\nReply-To: “.$name.” <".$from.">\r\nBcc: “.$bcc.”\r\nX-Mailer: PHP/".phpversion();"";
//add boundary string and mime type specification
$headers .= “\r\nContent-Type: multipart/mixed; boundary='PHP-mixed-”.$random_hash."’";
//read the attachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($finalimage)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>

–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

<?php echo $name; ?> sent you a jCard, click the attachment to view it.

Send your own jCards at www.j-cards.co.uk!

–PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/html; charset=‘iso-8859-1’;
Content-Transfer-Encoding: 7bit

<?php echo $name; ?> sent you this jCard:

Send your own jCards at j-cards.co.uk!

–PHP-alt-<?php echo $random_hash; ?>–

–PHP-mixed-<?php echo $random_hash; ?>

Content-Type: image/jpeg; name=‘jCard.jpg’
Content-Transfer-Encoding: base64
Content-ID: <<?php echo $random_hash; ?>.jCard.jpg>
Content-Disposition: attachment; filename=’/jCard.jpg’

<?php echo $attachment; ?>

–PHP-mixed-<?php echo $random_hash; ?>–

<?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully return success page html. Otherwise return send fail html. echo $mail_sent ? " snip...[/php] The email gets sent, and the [i]source[/i] of the email looks identical to previous correctly displayed versions, but for some reason the email reader no longer renders the html, instead it just shows the same text as the source but without headers - e.g.:
-PHP-mixed-643f51499c414383c5735473100462e4 Content-Type: multipart/alternative; boundary='PHP-alt-643f51499c414383c5735473100462e4' --PHP-alt-643f51499c414383c5735473100462e4 Content-Type: text/plain; charset='iso-8859-1'; Content-Transfer-Encoding: 7bit Joe sent you a jCard, click the attachment to view it. Send your own jCards at www.j-cards.co.uk! --PHP-alt-643f51499c414383c5735473100462e4 Content-Type: text/html; charset='iso-8859-1'; Content-Transfer-Encoding: 7bit

Joe sent you this jCard:

Send your own jCards at j-cards.co.uk!

--PHP-alt-643f51499c414383c5735473100462e4-- --PHP-mixed-643f51499c414383c5735473100462e4 Content-Type: image/jpeg; name='jCard.jpg' Content-Transfer-Encoding: base64 Content-ID: <643f51499c414383c5735473100462e4.jCard.jpg> Content-Disposition: attachment; filename='/jCard.jpg' /9j/4AAQSkZJRgABAQA... (base64 characters - very long - snipped) --PHP-mixed-643f51499c414383c5735473100462e4--
So everything is being sent, it's just an issue of getting it read correctly by the email program (worked before in gmail and hotmail). So I think it's an issue with the headers, but they look alright to me, I just can't see what I've done wrong. Any ideas?
Sponsor our Newsletter | Privacy Policy | Terms of Service