When it comes to communicating with your customers via your website, with automated e-mails, I’ve found that the following PHP script does the job nicely, and is easily adapted to suit your requirements.
For the script to run, using PHP’s built in mail function, you need to set-up four parts: the recipient’s e-mail, the subject line, the message itself and the mail headers.
Recipient’s E-mail
Generally speaking, this is something you’ll look up from a form submission or database entry. So, for that reason, we’ll assume that we have it loaded in as a string value, but for the purpose of this post, I’m going to define it manually:
1 | $recipientMail="g.attkins@frecosse.com"; |
Message Subject
Every e-mail really needs a descriptive subject line, so that user’s know whether to open it or not — if you don’t define this, you’ll end up having a blank entry where the subject should be in the recipient’s mailbox, and the chances are, the message will be consigned straight to the bin. Again, depending on how you implement this script, you could load the string value for the subject using different text. Here, I’ve manually defined it:
1 | $subject="Test Message from Website"; |
Message Body
This is the real meat of your e-mail, and should be written with the recipient in mind. I find that it helps to create a concatanated string, as it helps show the structure of the message, and allows for lines to be moved around more easily. For example:
1 2 3 4 5 6 | $body="Hi!\n\n"; $body.="This is a test e-mail, using PHP's built-in mail function.\n\n"; $body.="With any luck, it'll work first time!\n\n"; $body.="Best regards,\n\n"; $body.="Graeme Attkins\n"; $body.="Frecosse Website Design\n\n"; |
This first thing to notice is the use of the period before the equals sign in each of the lines following the first line, this tells PHP to join the strings together, in the order that it reads them. Alternatively you could write the e-mail message as:
1 2 3 4 5 6 | $body = "Hi!\n\n"; ."This is a test e-mail, using PHP's built-in mail function.\n\n"; ."With any luck, it'll work first time!\n\n"; ."Best regards,\n\n"; ."Graeme Attkins\n"; ."Frecosse Website Design\n\n"; |
It’s up to you to adopt the approach that suits you best!
You should have noticed the use of \n at the end of lines. This is the equivalent of
in an e-mail, and tells the mail function to put in line breaks. A double \n will give you a space between the lines, and a single \n will mean that the lines are right next to each other (as I’ve done in my sign-off).
I also like to put a couple of extra ones at the end of a message to ensure that all the message is seen on the screen, when a user scrolls down (remember, how your message appears is wholly dependent on the end-users mail client).
Talking of which…
Message Wordwrap
It’s generally considered best practice to limit the length of your messages so that they appear in lines that don’t stretch the entire length of the page. Now, you can either manually add \n wherever you want your lines to break, but this would be a tedious approach. Instead, consider using:
1 | $body=wordwrap($body, 70); |
Now, it’s important that you add this line after you’ve defined your body text, as what this does is take the body text, and split the lines before/after whole words with a maximum limit of 70 characters per line, which is the recommended length for ensuring that your text is nicely blocked. (I read this somewhere, but for the life of me can’t remember where!)
Message Headers
The final step before sending your message, is to sort out your headers. These tell the recipient’s e-mail client about the message, it’s content type, where it’s come from, who to reply to etc. Without these being set correctly, your message will almost certainly be classified as spam!
I tend to go with something along the lines of:
1 2 3 | $headers = 'From: Frecosse Website Design ' . "\r\n" . 'Reply-To: info@frecosse.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); |
It may seem odd to define the from e-mail twice (with it in reply-to as well), but this again, is simply best practice. By adding the From line as I have above, what the customer sees is Frecosse Website Design, but it’s from a valid e-mail address. The reply-to does allow you to set a different reply address to the from address, but I recommend against doing this, as I’m pretty sure it’s something that’s checked for by e-mail spam filters.
Sending the message
Now that you’ve set up your parts, you simple send the message by calling the mail function which is built into PHP:
1 | mail($recipientMail, $subject, $body, $headers); |
It’s as easy as that!
Final Script in Full
Below, I’ve listed the full script for your convenience:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $recipientMail="g.attkins@frecosse.com"; $subject="Test Message from Website"; $body="Hi!\n\n"; $body.="This is a test e-mail, using PHP's built-in mail function.\n\n"; $body.="With any luck, it'll work first time!\n\n"; $body.="Best regards,\n\n"; $body.="Graeme Attkins\n"; $body.="Frecosse Website Design\n\n"; $body=wordwrap($body, 70); $headers = 'From: Frecosse Website Design ' . "\r\n" . 'Reply-To: info@frecosse.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($recipientMail, $subject, $body, $headers); |

Zoe
February 11th, 2010 at 23:22
Hi,I’m taking some time to write you a comment. I hope you don’t mind I’ve bookmarked your page, your post is really usefull for me. Zoe x
Siobhan Greenway
February 20th, 2010 at 01:25
I was just searching the internet for this information. I thank you for your efforts that search has come to an end now. You wrote the post in a very understandable way.
Jacob Bradley
February 25th, 2010 at 10:12
Nice blog, keep up the good work and thank you for sharing.