A short article giving details and a tutorial on how to send emails to your fans, clients, or whoever using the PHP mail function and a HTML email.
Everyone who builds websites using PHP knows how to use the Mail function, in fact it is used by a lot of employers as a test of a potential employees ability to code in the language. In this article, I will provide you with a tutorial and examples of how to send HTML emails using the PHP Mail function. This is quite a simple function, and should take you no more than a few minutes to master, however like most developers, I don’t try to reinvent the wheel. Once you have built something once, you tend not to bother re-typing the same code unless you really have to.
1. Variables
The variables that you will need. In order to send an email using the PHP mail function, you will need to set various variables, which will form things like the email address the email has come from, the email address it is to be sent to, and the subject. In addition to this, you will need to set something called headers, which we will use to tell the mail function that we are sending an email function.
- $headers – This variable is explained further below.
- $email_to = “email@email.com”;
- $email_from = “FROM: noreply@christophereast.me.uk”;
- $subject = “test email”;
- $message = “Test emailTesting”;
2. Setting the Headers
Headers are the part of the email that tell it who it is from, and that the email is coded in HTML. Remember when you are sending an email in HTML, the message in the email must be coded in correct HTML4 otherwise errors may be displayed in some browsers. In-line CSS is recommended over separate CSS as it is then sent along with the email and doesn’t rely on other files to function. The following 2 sections of the header tell the mail function that you are sending an email with HTML content.
- $headers = ’MIME-Version: 1.0′ . “rn”;
- $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “rn”;
There are two things to remember when doing the above two lines of code. The first is that you need to include a full stop (.) before the equals sign on the second line, as this will append to the first line. The second is that instead of normal quotation marks, you only use a single quotation mark. This is because of the way PHP interprets code when it comes to the mail function.
3. Additional Headers
You can set some additional headers, which makes things a little bit easier to see in an email. These should be fairly self explainatory.
- $headers .= ‘To: Mary , Kelly ‘ . “rn”;
- $headers .= ‘From: Birthday Reminder ‘ . “rn”;
- $headers .= ‘Cc: birthdayarchive@example.com’ . “rn”;
- $headers .= ‘Bcc: birthdaycheck@example.com’ . “rn”;
The header section makes it easier for email clients to understand your emails, and make it easier for your emails to be checked for spam etc…
4. Putting It All Together
Once you’ve got it all, it’s time to put it all together. This final section allows you to actually get the emails sent, and provides you with the code you need to make it all work!
- mail($to, $subject, $message, $headers);
It really is as simple as that. So now you’ve got all the building blocks to a HTML email, you can go out and add it to your next website with no fuss whatsoever!













October 3rd, 2009 at 9:25 am
I use Wisywyg-it’s a lot easier LOL