PHP Mail

While sending emails through a script may sound advanced to you, its actually quite simple.

PHP has a function called mail() which allows you to send an email through your script.

Syntax

As with all other PHP functions, the mail function has its own syntax that must be followed, which is:

mail(to,subject,message,headers,parameters)

To

This defines the recipient of the email to be sent.

Subject

This defines the subject of the email to be sent.

Message

This defines the message of the email to be sent.

Headers

This is optional, and defines any additional headers to be used in the email (i.e. From, CC, BCC).

Parameters

This is optional, which defines any additional parameters to be used.

Sample Email Sent Using PHP

To send an email using PHP, all we need to do is define each variable that is to be used, then use the mail function to send the email.

$to = “friendsname@example.com”;
$subject = “My First Email”;
$message = “This is the first email I sent using the PHP mail function.n
This will be very useful to me.”;
$from = “yourname@example.com”;
$headers = “From: $from”;

mail($to,$subject,$message,$headers);
echo “Email sent successfully!”;

Notice how simple it is to send an email using PHP when all we have to do is follow the correct syntax of the PHP mail function.

First, we created the variables to be used, which are $to, $subject, $message, and $headers, which are all part of the mail syntax.

We then used the mail function and placed all of our variables in the correct order, then once the message was sent it would echo “Email sent successfully!”.

Notice how we used “n” in the message to create a new line. For more information, see PHP syntax.

Sending Emails Through A Form

It is not always possible to have each part of your email defined as a specific value. A good example to is when using a “contact me” type of form on your website.

When using such a form, the value of each variable will not be static, but will change each time a user submits new information through the form.

In such a case, we can create a simple form to gather the required variables from the user, then send them using the PHP mail function:

if (isset($_REQUEST['email']))
{
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( “yourname@example.com”, “Subject: $subject”,
$message, “From: $email” );
echo “Email sent successfully!”;
}
else
{
echo “<form method=’post’ action=’mail.php’>
Email: <input name=’email’ type=’text’ /><br />
Subject: <input name=’subject’ type=’text’ /><br />
Message:<br />
<textarea name=’message’ rows=’10′ cols=’25′></textarea><br />
<input type=’submit’ />
</form>”;
}

In the above example, we used a few different functions in our form.

You will notice the entire form is designed around an if/else statement.

The first order of business was to check if the email field was filled out. We did this using isset. Inside the mail() function, we then specified the email address to receive the message, then defined the variables $subject, $message and $email, which completes all of the required information needed to use the mail function.

Remember our syntax:

mail(to,subject,message,headers,parameters)

With the above form, we defined all of the required variables.

We then created a message to be displayed to the user once the email was sent using the echo function, which is:

Email sent successfully!

As part of our if/else statement, we then defined what would happen else the email field was left blank, which would be to display the form needed to gather all of the variables.