Wednesday 02 February 2011

Sending PHP Email From ApacheSending PHP Email From Apache

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 Apache.

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.

TEXT EMAIL



$headers = "From: WebServer\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\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";

$body =
"--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the HTML section\n\n"
."--".$boundary."--\n"
."--".$attboundary."--\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 boundary to enclose the text, 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\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";

$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));

$body =
"--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."--\n"
."--".$attboundary."\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\n"
."Content-Transfer-Encoding: base64\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\n\n"
.$attachment."\n"
."--".$attboundary."--\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\n"
."MIME-Version: 1.0\n"
."Content-Type: multipart/mixed; boundary=\"".$attboundary."\"\n\n";

$handle=fopen("filename.pdf", "r");
$content=fread($handle, filesize("filename.pdf"));
fclose($handle);
$attachment = chunk_split(base64_encode($content));

$body = "--".$attboundary."\n"
."Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n"
."--".$boundary."\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the text section\n\n"
."--".$boundary."\n"
."Content-Type: text/html; charset=\"iso-8859-1\"\n"
."Content-Transfer-Encoding: 7bit\n\n"
."This is the HTML section\n\n"
."--".$boundary."--\n"
."--".$attboundary."\n"
."Content-Type: application/octet-stream; name=\"my_attachment.pdf\"\n"
."Content-Transfer-Encoding: base64\n"
."Content-Disposition: attachment; filename=\"my_attachment.pdf\"\n\n"
.$attachment."\n"
."--".$attboundary."--\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

Hakan Says:

23rd June 2011 at 03:16

helped me alot, thanks a bunch! H.


Add your comments

Name

Comments

Question

7 + 4 = (this security question stops automated submissions)