I'm trying to set up a web application that sends an e-mail on completion to the user with an attachment. I want the e-mail to go in both HTML and text format to ensure that everyone can view it regardless of their mail settings. I'm having trouble getting it to work, so I was wondering if anyone can spot the problem with my code.
The code I'm using is below. This was taken as a template from another site, but I have done plenty of reading up so I have a rough understanding of how it all works. Unfortunately, not enough of an understanding to fix the problem!
$firstname = $_POST['firstname'];
$email = $_POST['email'];
//define the receiver of the email
$to = $email;
//define the subject of the email
$subject = "$firstname, thanks for using the One Minute Leveller";
//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
$headers = "From: The Xenon Group\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('Level3info.pdf')));
//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
TEXT E-MAIL GOES HERE. ACTUAL CONTENT REMOVED
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><h1>HTML CONTENT GOES HERE. ACTUAL CONTENT REMOVED</h1></html>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="Level3info.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?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( $to, $subject, $message, $headers );
Can anyone help?!