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:
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:
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";
<!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:
<?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.