Guide to PHP and MySQL (Buffalo State College Version)

Chapter 11: Sending Email with PHP

The PHP mail() functions provides a way to send an email message from your web site. To send your web server must be running an SMTP (Simple Mail Tranfer Protocal) mail server.

Using fake sendmail on a WAMP Server localhost

To send email via your localhost WampServer on Windows via a Gmail accont download fake sendmail for windows. Extract the file to your \www\sendmail\ folder. Then edit the sendmail.ini file and change these values:

Figure 11-1: sendmail.ini Settings

Edit the php.ini File

Next edit your php.ini file. Execute your phpinfo.php file from your localhost web server to find the full path to the Loaded Configuration File setting to find here the php.ini file resides. For the WAMPServer it might be C:\wamp64\bin\php\php5.6.25\phpForApache.ini on a 64-bit Windows machine or c:\wamp\bin\php\php5.6.25\phpForApache.ini on a 32-bit Windows machine (the PHP version may be different). Change the sendmail_path setting:

sendmail_path = "C:\wamp64\www\sendmail\sendmail.exe -t"
; might be: sendmail_path = "C:\wamp\www\sendmail\sendmail.exe -t"
Figure 11-2: phpForApache.ini Settings for fake sendmail

Sending Email with PHP

The PHP mail() functions provides a way to send an email message from your web site. A sample sendmail.php file:

$msg = "";
if (isset($_POST['emailTo'])) {
  $to       = "Administrator <>";
  $from     = "Jim <>";
  $subject  = "Email Message From: $from";
  $body     = "<html>\n";
  $body    .= "  <head>\n";
  $body    .= "    <title>Send an Email</title>\n";
  $body    .= "  </head>\n";
  $body    .= "  <body>\n";
  $body    .= "    <h1>Send an Email</h1>\n";
  $body    .= "    <p>Hello. Here is an email message sent using <code>sendmail.php</code></p>\n";
  $body    .= "    <pre>\n";
  $body    .= " Server IP: " . $_SERVER['SERVER_ADDR'] . "\n";
  $body    .= " Server Name: " . $_SERVER['SERVER_NAME'] . "\n";
  $body    .= " User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
  $body    .= " Script: " . $_SERVER['SCRIPT_NAME'] . "\n";
  $body    .= "    </pre>\n";
  $body    .= "  </body>\n";
  $body    .= "</html>\n";
  $headers  = "MIME-Version: 1.0\r\n"; // header lines always need \r\n
  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $headers .= "From: $from\r\n";
  $headers .= "Cc: Jim <>\r\n";
  mail($to, $subject, $body, $headers) or die("Mail Error!\n");
  $to = str_replace("<","<",$to);
  $to = str_replace(">",">",$to);
  $msg = "<p>Email message sent to: " . $to . </p>"\n";
<!doctype html>
    <title>Send mail php</title>
    <p><?php echo($msg); ?></p>
    <form id="sendMail" name=="sendMail" method="POST" action="<?php echo($_SERVER['PHP_SELF']);?>">
    <label for="emailTo">Email Address:</label>
    <input id="emailTo" name="emailTo" type="text" required/>
    <input type="submit" value="Send Email"/>

Figure 11-3: The PHP mail() Function

Here is the body of the email message that is received:

Send an Email

Hello. Here is an email message sent using sendmail.php

    Server IP:
  Server Name:
   User Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
       Script: /~gerlanjr/cis/475/beginning-php-mysql/sendMail.php
Figure 11-4: The mail() Function Output

Task - Send an Email Using PHP

For this task you will create a web form that accepts the first name, last name, and email fields. You should validate the fields using both JavaScript on the front-end and PHP on the back-end. Display appropriate messages if any fields are in error. When the form is submitted, and the data validated, your PHP back-end should send an email to the email address from the form and include the user-entered data ($_POST array) in the body of the message.

You will modify the PHP function for this item in your index.php page to add a link to your web site files that allows me to view your work.

You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2018 Jim Gerland