Sending email with PHP: from Basic to Advanced
- 2 hours on-demand video
- 1 article
- 30 downloadable resources
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to 4,000+ top Udemy courses anytime, anywhere.Try Udemy for Business
- 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
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.
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
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
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().
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.
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.
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.
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 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 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.
In this lecture you'll learn how to configure PHPMailer so you can add structure to the body of your emails using HTML.
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 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.
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 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 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.
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.
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.
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.
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
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.
- 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.
- 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.