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 sendmail.zip file to your \www\sendmail\ folder. Then edit the sendmail.ini file and change these values:

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=put-your-gmail-address-here
auth_password=put-your-gmail-password-here
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:

<?php
$msg = "";
if (isset($_POST['emailTo'])) {
  $to       = "Administrator <gerlanjr@buffalostate.edu>";
  $from     = "Jim <jim@jimgerland.com>";
  $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 <gerland@buffalo.edu>\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>
<html>
  <head>
    <title>Send mail php</title>
  </head>
  <body>
    <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"/>
    </form>
  </body>
</html>

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: 136.183.137.233
  Server Name: bscacad3.buffalostate.edu
   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 © 2016-2018 Jim Gerland