Sending email with PHP: from Basic to Advanced

Learn how to send emails from your PHP website, including best practices.
4.9 (22 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
137 students enrolled
$19
$30
37% off
Take This Course
  • Lectures 45
  • Length 2 hours
  • Skill Level All Levels
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 6/2016 English

Course Description

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.

What are the requirements?

  • 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.

What am I going to get from this course?

  • 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

What is the target audience?

  • 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.

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction and Project Setup
02:34

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.
03:03

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
03:28

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


02:27

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
Section 2: Using PHPMailer to Send email from PHP
03:02

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().


03:59

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.

02:05

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.

03:03

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
01:43

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.

3 questions

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

Section 3: How to use PHPMailer to send emails
01:56

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.

01:05

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.

02:30

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.

01:46

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.

01:00

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.

01:18

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.

02:31

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.

02:11

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
01:41

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.
5 questions

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

Section 4: Using HTML and CSS to add formatting to your emails
01:57

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

03:06

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
03:09

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.

03:55

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.

02:24

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.

02:19

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
6 questions

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

Section 5: Sending emails asynchronously: build a basic asynchronous queue from scratch
03:35

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.


04:25

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.
04:28

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.

03:23

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


04:21

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.

06:46

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.

02:22

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.

5 questions

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

Section 6: Sending emails asynchronously using the RabbitMQ Messaging Server
02:36

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.

00:59

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.

02:18

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.

02:17

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.

03:09

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.

01:34

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.

03:24

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.

04:44

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.

4 questions

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

Section 7: Third-party email services: Using Mailgun
02:15

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.

02:50

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.

05:04

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


03:19

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.

3 questions

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

Section 8: Conclusion
01:15

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
Article

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

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!

Ready to start learning?
Take This Course