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.
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:
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.
Welcome to the course!
In this lecture we'll look at:
In this lecture we'll install a web server locally on your computer. We'll cover:
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:
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:
We'll also learn why using a third-party package is the solution to the problems with mail().
In this lecture you'll learn how to install PHP code packages using the Composer package manager. Specifically:
We'll also look at how to install Composer and a simple working example of adding packages to a PHP project.
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.
In this lecture we'll configure PHPMailer with your SMTP server settings and connect to the mail server. You'll learn:
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.
Test your knowledge of the topics covered in the first section: Using PHPMailer to Send email from PHP.
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.
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.
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.
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.
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.
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.
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.
In this lecture you'll learn how to add a file attachment to an email with PHPMailer. Additionally:
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:
Test your knowledge of this section: How to use PHPMailer to send emails.
In this lecture you'll learn how to configure PHPMailer so you can add structure to the body of your emails using HTML.
In this lecture you'll learn:
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.
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.
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.
In this lecture you'll learn how to add a plain-text version of an email in PHPMailer. Also:
Test your knowledge of using HTML and CSS to add formatting to an email
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:
In this lecture we'll explore some options for getting a faster response from a web page when we send an email using SMTP:
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.
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:
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.
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.
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.
Test your knowledge of sending emails asynchronously with the basic queue system we built in this section.
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.
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.
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.
In this lecture we'll install PHP packages for:
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.
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.
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.
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.
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.
Test your knowledge of using the RabbitMQ Messaging software to send emails asynchronously.
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.
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.
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:
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.
Test your knowledge of using the third-party Mailgun email service.
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.
Hi, I'm Dave Hollingworth. I'm an IT trainer and web application developer. I've been programming for over twenty-five years, and teaching IT since 1999.
I've developed enterprise-level applications on platforms ranging from mainframes to mobiles: from my first web application, a quotes system created for a major insurance company back in 1999 - the first of its type in the sector - to interactive learning management systems for online language learning.
I've taught courses ranging from basic use of email to advanced web application development, both online and in-person. I've been teaching on Udemy since 2012.
I'm passionate about technology, and love teaching it! I believe it's possible to explain even the most complex subjects in a simple, straightforward way that doesn't confuse the student.
I look forward to welcoming you onto one of my courses!