Sending email with PHP: from Basic to Advanced
4.7 (158 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
1,076 students enrolled

Sending email with PHP: from Basic to Advanced

Learn how to send emails from your PHP website, including best practices.
4.7 (158 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
1,076 students enrolled
Created by Dave Hollingworth
Last updated 2/2018
English [Auto-generated]
Current price: $27.99 Original price: $39.99 Discount: 30% off
5 hours left at this price!
30-Day Money-Back Guarantee
This course includes
  • 2 hours on-demand video
  • 1 article
  • 30 downloadable resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Training 5 or more people?

Get your team access to 4,000+ top Udemy courses anytime, anywhere.

Try Udemy for Business
What you'll learn
  • Choose the right tool for sending email with PHP
  • Send emails from PHP to multiple recipients
  • Send emails from PHP containing non-English characters
  • Send emails from PHP containing images and with file attachments
  • Use HTML and CSS in your PHP emails to add formatting and layout
  • Use Composer to manage third-party code packages
  • Create an asynchronous queue from scratch to improve browser response time when sending emails
  • Use the RabbitMQ messaging server to build an asynchronous email solution
  • Use Mailgun to send, track and receive emails
Course content
Expand all 45 lectures 02:03:28
+ Introduction and Project Setup
4 lectures 11:32

Welcome to the course!

In this lecture we'll look at:

  • A summary of what you're going to learn throughout the course, starting from the basics of sending email through to advanced techniques.
  • How to get the most out of the course: experimenting with the code yourself, where to find the source code and how to get help.
Preview 02:34

In this lecture we'll install a web server locally on your computer. We'll cover:

  • Downloading and installing the software
  • Where to put your PHP files so you can view them in a web browser
  • Checking PHP is working by writing a simple script and viewing it in a browser
Preview 03:03

In this lecture we'll change the root directory of the web server so we can have code and resources outside of the publicly-accessible web folder. We'll cover:

  • How to make your code more secure by keeping some of it outside the web root
  • Reconfiguring the web server to change the root directory without changing the URL
  • Testing the new configuration

Preview 03:28

To follow along with the code, you'll need access to an SMTP mail server. In this lecture you'll learn:

  • What an SMTP mail server is and what it does
  • The data you need to connect to an SMTP server from code
  • Where to get access to a mail server
Preview 02:27
+ Using PHPMailer to Send email from PHP
5 lectures 13:52

In this lecture we'll learn about the built-in PHP mail() function, and why it's not a good choice for sending emails from PHP:

  • mail() requires a local mail server, which is difficult to configure
  • Code for formatting messages, adding attachments and so on is complex

We'll also learn why using a third-party package is the solution to the problems with mail().

Preview 03:02

In this lecture you'll learn how to install PHP code packages using the Composer package manager. Specifically:

  • Why you would use a code library
  • How you might use the library without Composer
  • What Composer does and how it simplifies this task

We'll also look at how to install Composer and a simple working example of adding packages to a PHP project.

Install PHP Code Libraries Automatically with Composer

In this lecture we'll install the PHPMailer library into our project. We'll also include the necessary files into our script and make sure it's working so we can start using PHPMailer.

Install the PHPMailer Library with Composer

In this lecture we'll configure PHPMailer with your SMTP server settings and connect to the mail server. You'll learn:

  • What each setting does and what value to provide
  • How to debug connection problems
Configure PHPMailer with Your SMTP Server

In this lecture we'll take the configuration settings out of our main script and put them in a separate file. This makes it easier to move our code between servers as we only have to change the settings in one place. It's also more secure as we can keep details such as username and password in a file outside of the web root.

Put Configuration Settings in a Separate File

Test your knowledge of the topics covered in the first section: Using PHPMailer to Send email from PHP.

Using PHPMailer to Send email from PHP
3 questions
+ How to use PHPMailer to send emails
9 lectures 15:58

In this lecture you'll learn the basic parts of an email: sender, recipient, subject and body. We'll run code for sending a simple email to one recipient using PHPMailer.

Preview 01:56

In this lecture we'll add optional names to email addresses, and see how they appear when the email is viewed in an email client.

Add a Name to an email Address

When sending an email, there are three ways you can send it to more than one recipient: to, cc and bcc. In this lecture you'll learn the difference between the three, and how recipients see the other recipients depending on how they've been sent the email.

Sending emails to Multiple Recipients: the Difference Between To, Cc

In this lecture we'll add code to send an email to multiple recipients using PHPMailer, in the to, cc and bcc fields of an email.

Send an email to Multiple Recipients using To, Cc and Bcc

In this lecture you'll see how normally when you get an email and click the reply button, the reply goes back to the original sender. It's possible to add a reply-to address to an email so that when you reply, it goes back to a different address.

Sending an email from One Address but Getting Replies to Another

In this lecture you'll learn the code for adding a reply-to address to an email in PHPMailer. We'll see how when you click reply in the email client, the recipient of the reply will be the reply-to address and not the original sender.

Send an email with a Different Address for Replies

In this lecture we'll see how adding non-English characters to an email can cause problems when the email is viewed in the email client. We'll see how we can fix this by specifying the UTF-8 character set in PHPMailer when we send the email.

Send email Containing Non-English Characters

In this lecture you'll learn how to add a file attachment to an email with PHPMailer. Additionally:

  • A common technique for getting the full path of a file in PHP
  • Recommended size limits for sending email attachments
Send an email with a File Attachment

In this lecture you'll learn how to change the name of a file when attaching it to an email. Reasons for doing this include:

  • The filename contains accented characters, punctuation or spaces (which might cause problems on some filesystems)
  • You might want to give it a filename that matches the date and time for example, or have a filename that includes someone’s name, ID number and so on.
Send an email with a File Attachment and Change the Filename

Test your knowledge of this section: How to use PHPMailer to send emails.

How to use PHPMailer to send emails
5 questions
+ Using HTML and CSS to add formatting to your emails
6 lectures 16:50

In this lecture you'll learn how to configure PHPMailer so you can add structure to the body of your emails using HTML.

Preview 01:57

In this lecture you'll learn:

  • The difference between an embedded, inline and attached image
  • How to add inline images to an email using PHPMailer
  • Why using inline images is unreliable and why some email clients block images
  • How to add an embedded image instead
Insert an Image into the Body of an email

In this lecture we'll summarise the three ways of adding CSS styles to HTML: external, embedded and inline styles. We'll also learn which method is the best for using in emails, and how to do this with PHPMailer.

Add Styles to the HTML in an email using CSS

The actual design of an email using HTML and CSS is outside of the scope of this course, but there are many online resources that we can use when writing emails that contain HTML and CSS that make the job easier, and might mean that us programmers don’t need to get a designer to do it for us. In this lecture we’ll to look at some of the most useful.

Using HTML and CSS in emails: Online Resources that Make it Easier

In this lecture you'll learn why it's important to send a plain-text version of an email in addition to a version in HTML.

Sending HTML email: Why it's Important to Send a Plain-text Version t

In this lecture you'll learn how to add a plain-text version of an email in PHPMailer. Also:

  • How to add line-breaks to the body of an HTML email
  • A way to convert your HTML email into plain text
Add a Plain-text Version of an HTML email

Test your knowledge of using HTML and CSS to add formatting to an email

Using HTML and CSS to add formatting to your emails
6 questions
+ Sending emails asynchronously: build a basic asynchronous queue from scratch
7 lectures 29:20

In this lecture we'll run some code to send an email using PHPMailer that shows how long it takes to run. We'll learn:

  • Why it takes a comparatively long time to send an email using an SMTP mail server
  • Why sending an email using SMTP is unsuitable when sending email interactively in a web browser.

Preview 03:35

In this lecture we'll explore some options for getting a faster response from a web page when we send an email using SMTP:

  • Adding visual feedback while the message is being sent
  • Running your own mail server
  • Sending the emails asynchronously.
Send email Quickly from a Web Page

In this lecture we'll start to build our own solution for sending emails asynchronously. We'll change our code so that instead of sending the email, it's saved to a text file, ready to be processed later by a separate script.

Queue emails to be Sent Later: Save emails in Text Files

In this lecture we'll change our code so that instead of explicitly requiring each external class, we use Composer's autoloader to load both:

  • classes from third-party packages
  • our own classes

Load all Classes using Composer’s Autoloader

In this lecture we'll add to code to process the queued emails, which have been saved in text files from before. You'll also learn how to run a PHP script on the command line instead of in a browser.

Send the Queued emails

In this lecture you'll learn how to run a PHP script automatically instead of having to run it manually.

You'll learn how to use cron (Mac and Linux) and Task Scheduler (Windows) to run a script according to a schedule you define.

We'll see code and a demo of the cron tool running a PHP script automatically.

Run a PHP Script Automatically at a Specified Time

In this lecture we'll add code to run the queue processing script on a regular basis using cron. The queued emails will be sent asynchronously so that the response in the browser is quick and isn't delayed by SMTP taking a long time.

Run the Queue Processing Script using cron

Test your knowledge of sending emails asynchronously with the basic queue system we built in this section.

Sending emails asynchronously
5 questions
+ Sending emails asynchronously using the RabbitMQ Messaging Server
8 lectures 21:01

In the previous section we fixed the problem of slow response when sending an email using SMTP by creating our own asynchronous queue. However, the queue solution we built isn’t suitable if we want to send more than just a few emails. Doing asynchronous processing like this using queues is very common in web applications, and lots of mature software exists for just this purpose.

One of the best messaging software is RabbitMQ. Although more complex than the solution we created in the previous section, it offers many advantages over that, such as being able to easily re-queue messages if there’s an error, greater reliability and speed, ability to scale when there are lots of messages and so on. In this section we’re going to install RabbitMQ and use it to process a queue of emails.

Preview 02:36

In this lecture we'll install the RabbitMQ server on your computer. Alternatively, if you don’t want to install it, it is available as an online service at CloudAMQP.

Install the RabbitMQ Messaging Server

In this lecture we'll discuss what data format to put on the queue. We'll see why using a language-neutral format is the best option, choose a popular, open format, and see how it's going to work with RabbitMQ.

Build a More Flexible Queue: Use a Language-neutral Message Format

In this lecture we'll install PHP packages for:

  • communicating with the RabbitMQ server
  • packing data using the MessagePack format

We'll use Composer in two ways to install the packages: first by editing the composer.json file and running the composer update command; and second by running the composer require command.

Install PHP Libraries for RabbitMQ and Message Pack with Composer

Now that we have our RabbitMQ server installed and ready to start receiving messages, in this lecture we'll start publishing messages to the queue, ready for processing later. We'll connect to the server, create a queue, pack some data and publish it to the queue.

Create a Queue and Publish a Message to it

With our previous solution, you could see the queued messages, as they were simple text files in a folder. Now however they’re inside RabbitMQ. To view them, we'll install the RabbitMQ Management Plugin and use it to view the queue and one of the messages.

View the Messages on the Queue

The script that processes the queue will need to do a lot of the same things as the script that’s sending messages to the queue, for example connecting to the RabbitMQ server and so on. Before we add that script, let’s extract the queue code out to a separate class so that we can avoid repeating ourselves.

Put the Queue Code in a Separate Class

Now we’ve got messages on the queue we need to process them. In this lecture we'll create the script to process the queued emails, and also learn how callback functions work in PHP.

Process the Messages on the Queue

Test your knowledge of using the RabbitMQ Messaging software to send emails asynchronously.

Sending email asynchronously with RabbitMQ
4 questions
+ Third-party email services: Using Mailgun
4 lectures 13:28

In previous sections we’ve covered in depth how to send email using an SMTP mail server. More specifically, our web server communicates with the mail server using the SMTP protocol. SMTP is easy to use, and third-party SMTP servers are freely available. However, there are alternatives. In addition to offering an SMTP service, Mailgun have an HTTP API.

In this lecture we'll get an introduction to Mailgun, and the advantages its HTTP API offers over using SMTP.

It's slightly more complicated to setup than using a simple SMTP server, as it requires configuring DNS settings, but once done it offers several advantages.

Preview 02:15

In this lecture we'll send an email using the Mailgun HTTP API. To do this first we need to install PHP libraries for using Mailgun and HTTP, then we'll call methods from the Mailgun API to send a simple email.

Send an email Using the Mailgun HTTP API

If you’re just sending using an SMTP server it can be difficult to know what happens to your emails once they’re sent. Mailgun allows you to track what happens to messages. In this lecture we'll:

  • Discover how you can track different types of events that happen to an email
  • See a working example of how to track hard bounces

Track What Happens to emails Once Sent

In addition to sending emails, it’s also possible to receive email using Mailgun. In this lecture we'll discover Mailgun routes, and how we can use them to process an incoming email. We'll see a working example of getting a webhook to send data to a URL when an email is received.

Receive email with Mailgun

Test your knowledge of using the third-party Mailgun email service.

Using Mailgun
3 questions
+ Conclusion
2 lectures 01:27

Congratulations on reaching the end of the course!

Now that you've successfully reached the end of the course, in this lecture we'll cover where to go from here.

Bonus Lecture: Get discounts on my other Udemy courses
  • You should already be familiar with PHP and HTML.
  • You should be comfortable installing software on your computer - in the first section we'll be installing a web server.
  • You should know the basics of using CSS to add styles to HTML. This course does NOT teach how to use HTML and CSS.

Learn the basic concepts, tools and functions that you will need to send emails from your website or web application in PHP.

Learn to Send emails using PHP in this Comprehensive Course.

  • Learn basic email concepts
  • Discover the best tools and methodologies for sending emails using PHP
  • Learn best practices to make sure your emails arrive as intended
  • Improve response time in the browser by sending emails asynchronously
  • Use third-party email providers to track what happens to emails once sent

Add Sending emails to Your PHP Toolkit

If you’re creating a website or web application in PHP, sooner or later you’ll want it to send emails to your users, for example: 

  • a contact page
  • an account activation email
  • a monthly report
  • an invoice
  • a password reset email
  • a newsletter
  • email marketing

and so on.

Sending email from PHP can be a tricky thing to get right. From making sure that your email is readable in as many different email clients as possible, to sending the email asynchronously so that your website responds as quickly as possible.

This course will teach you the basics through to the advanced techniques and tools used by professional PHP programmers.

Content and Overview

I designed this course to be easily understood by programmers who know PHP, HTML and CSS but don't have much experience sending emails from PHP.

Starting with the basic concepts of how emails work, such as senders, recipients, file attachments, using HTML and CSS and so on, this course will take you through all the steps needed to have your PHP website sending emails to its users.

Once we've covered the basics, we'll move on to advanced techniques like sending emails asynchronously and tracking emails using third-party email services.

New concepts are explained in detail as and when they're introduced, with full explanations of all the code used with detailed examples.

At the end of the course, you will be able to swiftly and reliably send professional-looking emails using PHP.

Complete with working source code at every stage, you'll be able to work alongside the instructor and will receive a verifiable certificate of completion upon finishing the course.

Who this course is for:
  • This PHP email course is for those who already know PHP and who want to learn how to send email from their PHP scripts.
  • This course is NOT for you if you don't already have some basic PHP and HTML knowledge.