Apache and IIS both interpret the mail() function differently. Here is how to send text email, HTML email, text email with an attachment, and HTML email with an attachment ... from a server running IIS.
This code must be copied EXACTLY as it is, including all \ " ' characters. Remember that a list of email addresses must be comma-separated, as semi-colons will result in errors. Obviously you will need to change the headers, or you will get errors from your SMTP server.
$headers = "From: WebServer\r\n";
$body = "This is the text section";
mail("test@test.com", "Text Email", $body, $headers);
$random_hash = md5(date("jS M Y", time()));
$boundary = "boundary-alt-".$random_hash;
$attboundary = "boundary-mixed-".$random_hash;
$headers="From: WebServer\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\r\n\r\n";
$body =
"--".$attboundary."\r\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\r\n\r\n"
."--".$boundary."\r\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."This is the text section\r\n\r\n"
."--".$boundary."\r\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."This is the HTML section\r\n\r\n"
."--".$boundary."--\r\n"
."--".$attboundary."--\r\n";
mail("test@test.com", "HTML Email", $body, $headers);
$random_hash = md5(date("jS M Y", time()));
$attboundary = "boundary-mixed-".$random_hash;
$headers = "From: WebServer\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\r\n\r\n";
$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$body =
"--".$attboundary."\r\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."This is the text section\r\n\r\n"
."--".$attboundary."\r\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\r\n\r\n"
."--".$attachment."\r\n"
."--".$attboundary."--\r\n";
mail("test@test.com", "Text Email with Attachment", $body, $headers);
$random_hash = md5(date("jS M Y", time()));
$boundary = "boundary-alt-".$random_hash;
$attboundary = "boundary-mixed-".$random_hash;
$headers="From: WebServer\r\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\r\n\r\n";
$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));
$body =
"--".$attboundary."\r\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\r\n\r\n"
."--".$boundary."\r\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."This is the text section\r\n\r\n"
."--".$boundary."\r\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."This is the HTML section\r\n\r\n"
."--".$boundary."--\r\n"
."--".$attboundary."\r\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\r\n\r\n"
.$attachment."\r\n"
."--".$attboundary."--\r\n";
mail("test@test.com", "HTML Email with Attachment", $body, $headers);
>> Posted in Websites at 16:02, and viewed 7,517 times
Next article: Bikes (26 June 2011)
Next Websites article: How To Identify Mobile Browsers Using PHP (30 March 2012)
Add your comments