It is a good practice to send HTML and TXT version of bunk emails. Some people just do not like HTML emails but they should still be able to get your message. The data comes from form in this example. As you can see from the code there is a post request which has submitted the form data to this page. You would probably want to include this code into a separate function or just forward the user away (on very simple site) from this page after the email has been submitted using the php header function.
if(isset($_POST['send_email'])){
// In this case I am only sending the email to 2 people and their email addresses are coming from the form
// Most likely you would get and array of email address and not only two so in that case you would loop through them and pass them into comma separated string
$strEmail1 = $_POST['email1'];
$strEmail2 = $_POST['email2'];
// Yours Email
$strYourEmail = $_POST['your_email'];
// Your Name
$strYourName = $_POST['your_name'];
$strSubject = "Test email";
$strTo = "$strEmail1,$strEmail2";
// Define the content (body of the email)
$strNoticeText = "This is a multi-part message in MIME format.";
$strHtmlText = "<html><body style=\"background-color:#000; color:#fff; padding:20px;\"><p>My test HTML email</p></body></html>";
$strPlainText = "My test TXT email";
// Email details
$strSemiRand = md5(time());
$strMimeBoundary = "==MULTIPART_BOUNDARY_$strSemiRand";
$strMimeBoundaryHeader = chr(34) . $strMimeBoundary . chr(34);
// Be careful not to include any space at the beginning of the lines of the text bellow
$strBody = "$strNoticeText
--$strMimeBoundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$strPlainText
--$strMimeBoundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$strHtmlText
--$strMimeBoundary--";
if (@mail($strTo, $strSubject, $strBody,
"From: " . $strYourName . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=" . $strMimeBoundaryHeader)){
echo "Email sent successfully";
}else{
echo "Error occurred ";
}
}
Comments
Good work! I think this could be made really useful by making it a more general function: something like (I’m just sketching the idea here)
function send_html_and_text_email( $to, $from, $subject, $htmlContent, $textContent = ""){ // $to could be an array or a single email, the function would be able // to deal with it using the is_array function // The text half of the email, if the parameter is empty, could be extracted // from the html content, by extracting everything between the <body> tags // with a regex and then running strip_tags on that }Like I say, it’s just a sketch, but could be a really useful library function
Also, one thing I can’t help but note from your code is the mix of Camel Case and underscore convention for naming variables, sometimes even within the same identifier – that’s a bit mental
Looks great @Griff.
Good call with those variables. I put the code together from various sources and that’s where the mixture of variables come from. Let me fix that.