Mail Form PHP Script

This is a script to process form data and e-mail it to a specified e-mail address. It works only with the POST method.

Each field in the form is put into the e-mail with the appropriate value, and sent to the specified e-mail address. The sending address can be pre-defined or provided from a field within the form data. Once the message has been sent, the user is redirected to either a success or fail page. The script has a number of customizable options, see "Using this Script" below.

Sample E-Mail

An e-mail sent using this script will look something like:

To: youraddress@yourdomain.com
From:
noreply@domain.com
Subject:
A user has filled in your form
-- Message Body --
The following form data was submitted:

FirstName: John
LastName: Doe

Download

The script is available in a zip file which contains the PHP script, as well as a sample form, success and fail pages.

php_mail_form.zip (11kb)

Using this Script

The following explains how to implement this script in your site.

  1. Download the script and open it in your favourite text editor.

  2. Modify the "E-Mail and Page options" section to meet your needs (see below for more information).

  3. Either modify the existing sample form, or create your own, make sure that the action parameter points to the location of the script and that the method is set to POST.

  4. You should be done, note that the success and failed HTML files, unless specified by a leading "/", are relavtive to the location of the PHP script.

E-Mail Options

Most of the options in the script are self-explanatory provided you understand the PHP coding to set them, this section covers just that.

$email_to and $email_bcc

These are arrays in which you can enter as many e-mail addresses as you wish. E-mail addresses in the $email_to field will show up to all recipients, addresses in the BCC field will not.

$email_to  = array ( "addy1@dom.com", "addy2@dom.com" );
$email_bcc = array ( "addy1@dom.com", "addy2@dom.com" ); 

If you want to leave a field blank, simply set it's value as "".

$email_bcc = array ( "" ); 

You can specify as many e-mail addresses as you want, separating each one with a comma.

$email_from_form and $email_form_field

If you want the e-mail to appear to be sent from a field from the form data, set $email_from_form to true and specify the name of the field in $email_form_field. The field name should match exactly the field name in the form objects "name" parameter, it is case sensitive.

$email_form_send and $email_bcc_form

If you want to send a copy of the e-mail to e-mail field specified in the $email_form_field option, set $email_form_send to true.

You can send the e-mail as "to" in which case all other recipients will see the message has been delivered to this person, or you can use the BCC method to hide this fact. Use $email_bcc_form to specify this.

$eol

"$eol" allows you to specify the end-of-line characters used to build the e-mail. These are usually standard and the default value should work fine. This is thrown in for compatibility as I read that on some Windows servers you need to change this to just "\r".