Sending Email: Free PHP & MySQL Tutorial

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this PHP & MySQL tutorial:

Installing MailHog, Setting up MAMP Pro, Setting up XAMPP, Sending a test email

Exercise Overview

One of the great things about PHP is how easy it is to send an email. However, setting up a mail server in a testing environment is not always so straightforward. In this exercise we’ll show you how to set up MAMP Pro or XAMPP to send test emails, as well as how to actually send the email with PHP.

Getting Started with MailHog

MailHog is a mail catcher, which means it catches any mail sent to it, and displays it.

If you are in a Noble Desktop instructor-led class, we have already installed and configured MailHog for you. If you’re going through this workbook on your own at your office or home, install MailHog using the sidebar for your operating system.

Installing MailHog on Mac

  1. Open Terminal.

  2. Install the Xcode command line developer tools by typing the following command and hitting Return:

    xcode-select --install
    
  3. If you get a pop-up asking if you would like to install the tools now, click Install.

  4. Click Agree to the terms of use. It may take a few minutes to install.

  5. Next we need to install Homebrew, a command line tool that makes it easy to install popular software packages (like MailHog) on macOS. In your web browser, go to http://brew.sh

  6. Copy the command prompt under Install Homebrew.

  7. Paste the command into Terminal.

  8. Hit Return and follow the prompts.

  9. If you get a message that Homebrew is already installed, type the following command and hit Return:

    brew update
    
  10. Install MailHog by typing the following prompt and hitting Return:

    brew install mailhog
    

Installing MailHog on Windows

  1. In your browser go to github.com/mailhog/MailHog

  2. Scroll down to Getting started.

  3. Click on Download the latest release.

  4. Click on the latest release number. As of the writing of this book, the newest release is v1.0.0.

  5. Under Downloads click MailHog_windows_amd64.exe (64-bit system) or MailHog_windows_386.exe (32-bit system).

  6. Save the file in your Program Files in a new folder called MailHog.

Running MailHog on Mac

If you’re a Windows user, skip to the next section.

  1. Open Terminal.

  2. Type the following command:

    mailhog start
    
  3. Hit Return.

  4. Leave Terminal open in the background.

Running MailHog on Windows

  1. Navigate to Program Files > MailHog and find the .exe file that starts with MailHog.

  2. Double–click it and hit Run to open it in the Command Prompt.

  3. If you get an alert that Windows Firewall has blocked access to some features of this app, click Allow access.

  4. Leave Command Prompt open in the background.

Setting Up Email: MAMP PRO

If you’re a Windows user, skip to the next section.

  1. Switch to MAMP PRO.

  2. Before we can use MailHog, we’ll have to edit the php.ini file. This is a configuration file that is read when PHP starts up.

    To access the file, go to File > Edit Template > PHP (php.ini) and choose the version of PHP you are using in MAMP.

    NOTE: The PHP version you are using will be listed in MAMP Pro’s main window under the PHP Version column.

  3. If you get a warning, click OK.

  4. To send emails, we need to edit the sendmail_path. We’ve prepared a file for you with the path we need to use. Switch to your code editor to open sendmail_path.txt from the phpclass folder.

  5. Select the line and copy it.

  6. Switch back to the php.ini file in MAMP Pro.

  7. There will already be a commented out ;sendmail_path we can replace. Scroll down until you see the following lines of code around line 570:

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path =
    

    NOTE: If you cannot find it, use Cmd–F to search for sendmail_path.

  8. Select ;sendmail_path and paste the new line to replace it as shown below:

    sendmail_path = /usr/local/Cellar/mailhog/1.0.0/bin/MailHog sendmail
    
  9. Notice that we used 1.0.0. As of the writing of this book, the newest release of MailHog is v1.0.0. If you are using a newer version, you’ll need to edit the sendmail_path with the correct version number. An easy way to check which version you are using is to copy the following section from the sendmail_path and paste it in your browser:

    /usr/local/Cellar/mailhog/

    You’ll see the version number listed next to the folder icon.

  10. Back in php.ini, edit the path if necessary and save the file.

  11. Close the file.

  12. MAMP Pro will ask you whether to restart the servers. Click Yes.

Setting Up Email: XAMPP

Before we can use MailHog, we’ll have to edit two configuration files: php.ini and sendmail.ini

  1. Go to the XAMPP control panel.

  2. Next to Apache, click the Config button and from the menu choose PHP (php.ini).

  3. This will open up php.ini in your default text editor.

  4. Press Ctrl–F to open up the Find window.

  5. Next to Find what, type sendmail and click Find Next.

  6. You should be taken to the following lines of code:

    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = postmaster@localhost
    
  7. Scroll down a few lines until you see the following code:

    ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
    ;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    
  8. By default XAMPP writes our emails to the disk instead of sending out the email. Let’s change that. As shown below, delete the semi-colon (;) before the sendmail line and add a semi-colon (;) before the mailtodisk line.

    ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    
    ; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
    ;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
    
  9. Save the file.

  10. Close the file. You should be back in the XAMPP Control Panel.

  11. Switch back to your code editor.

  12. Go to File > Open File.

  13. Navigate to the C: > xampp > sendmail folder.

  14. We need to edit sendmail.ini. If you see the file, double–click it to open it.

    If instead you see two sendmail files without extensions, find the File name field at the bottom of the Open window, type sendmail.ini then click Open.

  15. Around line 14, find the following lines of code:

    smtp_server=mail.mydomain.com
    
    ; smtp port (normally 25)
    
    smtp_port=25
    
  16. Edit the code as shown below:

    smtp_server=localhost
    
    ; smtp port (normally 25)
    
    smtp_port=1025
    
  17. Save the file.

  18. Close the file.

  19. Switch back to the XAMPP control panel.

    We need to restart the Apache server for the change to take effect. If Apache is installed as a service (there is a green checkmark to the left of Apache), you will also need to restart your computer.

  20. To the right of Apache click the Stop button.

  21. To the right of Apache click the Start button.

    NOTE: If Apache does not start up (or it starts then immediately stops), you will need to restart your computer. After you restart your computer, relaunch XAMPP. The servers should start up without a problem.

Sending an Email

Now that we have a test mail server set up, we can actually go about sending an email. Sending a simple mail message with PHP is very easy: just use the mail() function.

The mail() function is formatted like this:

mail(to, subject, message, headers, additional parameters)

  1. In your code editor, open mail.php from the phpclass folder.

  2. Add the following at the top of the document:

    <?php mail(
          "youremail@gmail.com", 
          "Hello World", 
          "Hello, this is a test msg!", 
          "From:youremail@gmail.com\r\n"
       );
    ?>
    

    Note also the \r\n after the From address. This is a carriage return and a line feed required by many email servers. Your email may not get sent if you leave it out.

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/mail.php
    • Windows: localhost/phpclass/mail.php

    If you pull up Terminal or Command Prompt, you can see the status of your email being sent.

  4. Time to check our email! In your browser, go to localhost:8025

  5. The MailHog interface will open, showing an inbox. You should see your new email listed in the inbox.

  6. Close any open files. We’re done for now.

Yelp Facebook LinkedIn YouTube Twitter Instagram