The php mail() function. Such a simple function with so much versatility. Now I know, I know.. trust me I know! There are dozens of tutorials out there on the subject of PHP mail() and its functionality. But how many of them are based off the core concept, and how to use it statically at best with hard coded variables. Example…
$email = "myemail@exmple.com";
$subject4email = "This is a Subject";
$message4email = "Thanks for triggering this email to be sent";
mail($email, $subject4email, $message4email);
How many “php mail” tutorials have you seen like that today while searching for your solution? Chances are they are long winded but up the ally of just giving you the above mentioned code.
Well with this short post I intend on showing you a way many people spend hours searching for to end up sitting on forums eventually waiting for an answer that may never come. PHP’s mail() function is great. But as mentioned above almost all people who give tutorials on the subject show the most core basic concept. Which is fine if you plan to send your self emails in plain text. What I want to show you here is based on the ever growing demand for wanting to send something a bit fancier to your users or even yourself (if your like me, even personal statistics and logs have to look ascetically pleasing even if its only me who will ever see it). So in this post I will give you the core basics of how to code a HTML based template using CSS and standard HTML for use with Email and being sent through PHP. Mind you basic. Im not going to provide you with a fully custom tailored solution. I want to leave it open a bit so you can build on it with ease. This example could be used for example if you have an automated mail to go to new members on your site but its far from limited to the idea of just that.
Full Code:
$mydomain = "somedomain.net";
$username = $selectusers;
$email_who = $selectusersemails;
$titlesubject = $mymessagetitle;
$messagebody = $mymessage;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>' . $titlesubject . '</title>
</head>
<style type="text/css">
<!--
body {background: #edebea;}
#wrapper {background: #fff;border: 4px solid #ddd;}
-->
</style>
</head>
<body><table width="650" id="wrapper">
<tr>
<td>Your Sites Image Logo or slogan or text or link</td>
<td>Dear '.$username.'</td>
<td>'.$messagebody.'</td>
<td>Footer information, copyrights, other..</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=us-ascii' . "\r\n";
$headers .= 'From: noreply@'. $mydomain . "\r\n";
$headers .= 'Reply-To: noreply@'. $mydomain . "\r\n";
$headers .= '1\r\nX-MSMail-Priority: High' . "\r\n";
$headers .= 'X-Mailer: Monkey Tooth Productions Mailer v1.0.0' . "\r\n";
mail($email_who, $titlesubject, $message, $headers);
Now with the above You can see sending an HTML based email through PHP’s mail() function is not as hard as one would have thought, yet its a pain in the arse to find information on (or was when I last looked some time ago, but then again I still see it asked in forums a lot so either way I suppose).
Now as you can see as I mentioned before this is thee absolute core concept of sending an HTML based email with CSS with PHP
$username = $selectusers;
$email_who = $selectusersemails;
$titlesubject = $mymessagetitle;
$messagebody = $mymessage;
The above four variables can be set to anything and set through any means you choose whether is something through a database query or hard coded in to a combination of the two. However only two of the four above are required with the above mentioned script. Those being $titlesubject and $email_who but you can populate them however you wish. The other two $messagebody and $username are solely for the sake of example within the $message variable.
Now its worth mentioning as you can see I added a variable for the domain $mydomain All though you could use anything its suggested that you keep it specific to your domain the main reason I added this one variable is if you look at the script you will notice that its called a few times within the headers for example. I figured one place to edit it is easier than several. Main reason you want to keep it specific to your domain is well despite what you change it to the email ISP provider will be able to track it back to your domain. So making it something different will only trigger the email provider to throw it in the spam folder. All in all since I mention the spam concept It is possible to have all your emails land directly in the spam box of any give email you send But that will almost always be due to your choice of $messagebody variable in this example. I say this because I have been using this concept for years now with very little feedback of my messages landing in someone spam bin. Mind you I specify mine as I am careful with my messages. I keep them low key with little to no references that can trigger a spam filter into throwing the email into a spam folder.
Prevention Tips try not to use to many links in your emails sent if you have to try to use full urls as you would for the link itself to go to as the links text as well if they match its good use a spell check on your message prior to sending to many misspelled words can be bad to.