PHP includes the mail() function for sending email. The function is defined below:
mail(string to, string subject, string message,string additional_headers) -
allows you to send mail. Returns true if the message is sent successfully
otherwise a value of false is returned.
The following example demonstrates how to use the mail() function:
$to = 'email@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: firstname.lastname@example.org' . "\r\n" .
'Reply-To: email@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
The first step is to create a variable to hold the email address to which the message will be sent:
This could be any vaild e-mail address. Multiple e-mail addresses should be separated using a comma ",".
The $subject variable contains the subject of the e-mail. This string will appear in the subject line of the message.
The main contents or body of the email message is assigned to the $msg variable. If necessary it is possible to concatenate several $msg variables together. This is often required when long descriptive messages are sent.
$msg = "Message generated using PHP main() function.";
Next, the mail headers are built and assigned to the $mailheaders variable. Mail headers are the strings at the beginning of e-mail messages that formulate their structure and essentially make them valid mail addresses. While the mail() function can be used without headers, it is recommended that the "From:" and "Reply-TO:" headers be included.
$mailheaders = "From: My Web Site ";
$mailheaders .= "Reply - To: firstname.lastname@example.org";
Finally, the mail() function is called to send the message:
In most cases, the to, subject, and messages parameters of the mail() function are not simply hardcoded as shown in the previous example. Instead they are supplied dynamically as a result of user input. For example, consider a page that allows a user to electronically register for a product or service. The user enters a first name, last name, email address, and telephone number. This information is passed to a PHP page that parses the information and sends the user a confirmation e-mail. The following examples demonstrate this process:
<title>A Web Page</title>
<h3> Registration Page </h3>
<form name="registration" method="post" action="email.php">
First Name: <input type="text" name="fname">
Last Name: <input type="text" name="lname">
Email Address: <input type="text" name="email">
Telephone: <input type="text" name="telephone">
<input type="submit" name="Submit Registration">
The registration.htm page is a standard HTML form page that allows the user to enter a first name, last name, e-mail address, and telephone number. When the "Submit Registration" button is clicked, the form data is passed to the PHP page - email.php as PHP $_REQUEST variables:
$_REQUEST['fname'] - contains user's first name
$_REQUEST['lname'] - contains user's last name
$_REQUEST['email'] - contains user's email address
$_REQUEST['telephone'] - contains user's telephone number
The following script shows how the form information is parsed and used by the mail() function:
$to = $_POST[email];
$subject = "Registration Confirmation";
$msg = "Dear: " . $_POST[fname] . " " . $_POST[lname] . ",\n\n";
$msg .= "You are now successfully registered.
$mailheaders = "From: Registration Site <email@example.com>";
$mailheaders .= "Reply - To: firstname.lastname@example.org";
mail($to, $subject, $msg, $mailheaders);
The email.php pages assigns the values of the $_REQUEST superglobals (containing the values submitted from registration.htm) and assigns them to scalar variables which will be easier to work with. Next, mailheaders are created and the mail() function is called. The scalar variables supply the function with the required parameters.