A nice commandline PHP emailer (can be adapted for sites), derived primarily from code at http://php.net.
Worthy of note:
- Use of $HOSTNAME to get the name of the host. This is good for commandline when the $_SERVER variables aren’t available.
- Separate text and HTML source files, normally, text would be derived from HTML with strip_tags.
PHP:
<?php | |
| |
if ($argc < 4) | |
die('Usage: <destination email address> <text file> <html file>'."\n"); | |
| |
$sHost=trim(shell_exec('echo $HOSTNAME')); | |
$fromname='no-reply'; | |
$fromaddress='no-reply@'.$sHost; | |
| |
$sTo=$argv[1]; | |
$sTextFile=$argv[2]; | |
$sHTMLFile=$argv[3]; | |
| |
$boundary='=_'.time(); | |
| |
$plain=file_get_contents($sTextFile); | |
| |
$html=file_get_contents($sHTMLFile); | |
| |
/* Courtesy of: <a href="http://www.php.net/manual/en/ref.mail.php#77405">http://www.php.net/manual/en/ref.mail.php#77405</a> - Modified slightly */ | |
| |
$eol="\r\n"; | |
$mime_boundary=md5(time()); | |
| |
# Common Headers | |
$headers .= "From: ".$fromname."<".$fromaddress.">".$eol; | |
$headers .= "Reply-To: ".$fromname."<".$fromaddress.">".$eol; | |
$headers .= "Return-Path: ".$fromname."<".$fromaddress.">".$eol; // these two to set reply address | |
$headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol; | |
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters | |
| |
# Boundry for marking the split & Multitype Headers | |
$headers .= 'MIME-Version: 1.0'.$eol; | |
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol; | |
| |
# Open the first part of the mail | |
$msg = "--".$mime_boundary.$eol; | |
| |
$htmlalt_mime_boundary = $mime_boundary."_htmlalt"; //we must define a different MIME boundary for this section | |
# Setup for text OR html - | |
$msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol; | |
| |
# Text Version | |
$msg .= "--".$htmlalt_mime_boundary.$eol; | |
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol; | |
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; | |
# $msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol; | |
$msg .=$plain.$eol.$eol; | |
| |
# HTML Version | |
$msg .= "--".$htmlalt_mime_boundary.$eol; | |
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol; | |
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; | |
$msg .= $html.$eol.$eol; | |
| |
//close the html/plain text alternate portion | |
$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol; | |
# Finished | |
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. | |
| |
# SEND THE EMAIL | |
$mail_sent = mail($sTo, "Test From $sHost", $msg, $headers); | |
| |
?> |
Risks of HTML emails: http://www.mailchimp.com/resources/top10_html_email_mistakes.phtml
Recommending testing prior to production release
Receipt of emails on all major email clients
http://fingerprintapp.com/email-client-stats