Wednesday 02 February 2011

Sending PHP Email From IISSending PHP Email From IIS

Left QuoteApache 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.

TEXT EMAIL



$headers = "From: WebServer\r\n";
$body = "This is the text section";
mail("test@test.com", "Text Email", $body, $headers);


HTML EMAIL



This email sends both plaintext and HTML. If the email client cannot interpret the HTML, it will display the plaintext. The code uses a boundary to separate the plaintext from the HTML, contained in an attachment boundary.

$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);


TEXT EMAIL WITH ATTACHMENT



This script reads the file filename.pdf and attaches it as my_attachment.pdf, so ensure that filename.txt is in the right directory. It uses a attachment boundary to separate this from the actual attachment, but as IIS is slightly more lenient than Apache, we do not need an attachment boundary container.

$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);


HTML EMAIL WITH ATTACHMENT



This email sends plaintext, HTML and an attachment. The code uses a boundary to separate the plaintext from the HTML, and an attachment boundary to separate this from the actual attachment.

$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);
Right Quote

Next article: Bikes (26 June 2011)

Next Websites article: How To Identify Mobile Browsers Using PHP (30 March 2012)

CommentsComments

Add your comments

Name

Comments

Question

5 + 2 = (this security question stops automated submissions)