I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

Sending A Simple eMail With PHP

php / email

Estimated reading time: 2 minutes, 21 seconds

For low-traffic websites, it is reasonable to be informed by eMail when a site’s form is submitted. In principle, sending eMail with PHP couldn’t be simpler: it is a single function, mail, that has just three required arguments: to, subject and message.

<?php mail(, 'Subject Line’, 'The body of the eMail message’); ?>

This works for the simplest of messages. But before we proceed, there are several important points to note:

  1. mail relies on a mail server being installed and operational. This is not the default for most of the self-installed server options I recommended earlier. You may want to test your uses of mail from your actual hosting provider.

  2. Many hosting providers will restrict or complicate the use of the mail function in order to avoid poorly written pages being used as spam gateways.  It is your responsibility to investigate any hurdles that may be unique to your hosting setup.

  3. When testing your eMail routine, make sure you can receive sent eMails promptly and correctly. Certain eMail services update their inbox less frequently; others may have over-zealous spam filters. In my experience, gMail is the best option.

  4. If your form is submitted more than a dozen times a day, you will need to look at a different way of handling the incoming data (e.g. direct insertion of the information into a database).

  5. Use of the mail function is not the way to handle eMail campaigns. For that, you’ll likely want to use a service such as MailChimp.

  6. Mail sent with the mail function is shown as being sent from the eMail address of the server administrator. Changing this, and a lot more besides, is the remit of a fourth, optional value, headers.

First, the arguments that we have so far can easily be substituted with variables from a form:

<?php $to = '';
$subject = 'New form completion';
$message = $_POST['comment'];
mail($to,$subject,$message); ?>

(This, of course, assumes that the form information has been validated first – you don’t want to receive an eMail every time the form is filled out incorrectly).

Because the message portion can become very long, it is common to join components together to form the entire message, using a special form of concatenation, along with escaped line returns (note the use of periods and spaces immediately after the second use of the $message variable):

$message = 'New message from '.$_POST[firstname].' '.$_POST[lastname].': \r\n';
$message .= $_POST['comment'];

The result, sent in the body of the eMail, would include the supplied first and last name of the person who filled out the form, together with their message:

New message from Harry Belafonte:

Day day light an me wan’ go home

For anything more complex than this: sending a "rich" HTML eMail, or showing the eMail as being sent from the person submitting the form, rather than the eMail administrator, we need to modify the eMail headers.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.