Sending E-mails

category: Advanced
Created by: Dr.Ferrous

Sending E-mails

category: Advanced
Created by: Dr.Ferrous
twitter google+ facebook pinned 

To send e-mails directly from a PHP script, you can use the mail() function.


  • to - a string with the address(es) of the receiver(s). Can be in either of the following formats:
                    "[email protected]"       or       "Some Name <[email protected]>"
    To send to more than one address, use a comma-separated string like this:
                    "[email protected], [email protected], Some Name <[email protected]>"
  • subject - the email's subject line.
  • body - is where you put the contents of the email. It must be presented as a single string. New lines must use both a carriage return (\r) and newline character (\n), "\r\n", and the lines should not be larger than 70 characters. To wrap text to 70 characters, you can use wordwrap() function. It adds a new line every X characters: $body = wordwrap($body, 70);
  • headers - optional, it's a string used to add extra headers (From, Reply-To, Cc, Bcc, encoding). Each new header, except the final one, should be separated with a "\r\n". It is recommended that you always include a From value in the "headers" parameter.

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

This function doesn't send the email itself. It just transfers email data to a mail server running on the computer, and actually that server sends the email. The computer on which PHP is running must have a working mail server in order for this function to work. But PHP has no way of knowing if the email is delivered to the destination.

Simple form contact script

Usually, data for the mail() function arguments, especially for "body", is received from an HTML form.

Here is an example of a simple php mail script for a contact form. Sends a text mesage from a form to a specified e-mail address.

- First, this script checks if the "email", "subject" and "message" fields are filled out. Gets data from these fields and use the mail() function to send the email, and returns a message for success or error.
- $to variable contains the address where the e-mail will be sent.

Validate email address, filter form data and set headers

The script above is the simplest way to send e-mail, but it is not secure. For example, it should check if the user has completed all fields, also, to validate email address, and other filters.

By default, mail() uses Latin1 (ISO-8859-1) encoding, which doesn't support accented characters. But the Unicode (UTF-8) supports most written languages, including the accents commonly used in European languages.

To ensure that email messages display correctly accents and less usual characters, use the Content-Typeg header to set the encoding to UTF-8 like this:
                $headers = "Content-Type: text/plain; charset=utf-8\r\n";
Another usefull header is Reply-To, this set an address for Reply.
                $headers = "Reply-To: [email protected]";

Here is a more complete and secure contact form script. It removes tags from form data, checks to be a valid e-mail address, and a minimum number of characters for "subject" and "message". Also, it adds the "From", "Conten-Type" (utf-8), and "Reply-To" headers in the email (for more explanations, see the comments in the script).