Sending Email

This section describes how to use SMTP services to send automated e-mail messages from PHP applications. Email is sent from a Web server through its SMTP (Simple Mail Transfer Protocol) service. This is, as the name implies, a limited email service; however, it is sufficient for generating automated emails. It should be noted that an SMTP server is required in order to take advantage of PHP's e-mail functionality. In Windows XP Professional, Windows 2000 server, and Windows 2003 server, SMTP services are bundled with Internet Information Services (IIS). On Linux/Unix, Sendmail and Qmail are popular SMTP packages.

If your are running PHP under a server using IIS SMTP services, you may need to configure it to permit relay of email messages. Perform the following steps:

1. Open IIS Administrative Tools.
2. Stop Default SMTP Virtual Server service.
3. Open properties window of Default SMTP Virtual Server.
4. Click "Access" tab and click "Relay..." button.
5. Click "Only the list below" button and add the single computer at IP address 127.0.0.1.
6. Click "OK" buttons to close "Access" tabs and properties window.
7. Restart Default SMTP Virtual Server service.

It is also necessary to make the following changes to the PHP configuration file - php.ini - so that PHP can use SMTP services. Open the php.ini file with a text editor and locate the following lines:

[mail function];
For Win32 only
SMTP = localhost
;For Win 32 only
sendmail_from = me@localhost.com

You will need to modify the SMTP directive to point to your SMTP server. If you are using local SMTP services, this value should be set to localhost. The second directive sendmail_from is the email address used in the From header of the out-going e-mail. This should be set to a valid e-mail account if users will be permitted to respond to auto generated e-mail messages.

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:



<?php

$to      = 'youraddress@domain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@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:

$to ="youraddress@domain.com";

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.

$subject="PHP Mail";

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 <myaddress@mydomain.com>";
$mailheaders .= "Reply - To: myaddress@mydomain.com";


Finally, the mail() function is called to send the message:

mail($to,$subject,$msg, $mailheaders);

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:



<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<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"/>

</form>

</body>
</html>

The registration.htm page is a standard XHTML 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 $_POST[] variables:

$_POST['fname'] - contains user's first name
$_POST['lname'] - contains user's last name
$_POST['email'] - contains user's email address
$_POST['telephone'] - contains user's telephone number


The following script shows how the form information is parsed and used by the mail() function:



<?php

$to = $_POST[email];
$subject = "Registration Confirmation";
$msg = "Dear: " . $_POST[fname] .  " " . $_POST[lname] . ",\n\n";
$msg .= "You are now successfully registered."

$mailheaders = "From: Registration Site <myaddress@mydomain.com>";
$mailheaders .= "Reply - To: registration@mydomain.com";

mail($to, $subject, $msg, $mailheaders);

?>


The email.php pages assigns the values of the _POST[] 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.