Understanding email
In this lesson you will get a high-level overview of how email works on a Linux server and in WordPress. We'll look at the different components involved in sending a message, how WordPress hands mail off to the system, and why a local mail transfer agent like Postfix is the preferred way to ensure messages are accepted, queued, and delivered.
Email is often a crucial part of a WordPress application, especially when working with e-commerce and memberships: account activation, password reset, order notifications, etc. Managing a server is often dependent on email too: disk space alerts, security notifications, and more.
You don't need to be an expert in email, but it will certainly help to understand how email works in general, and how to configure your server to handle outgoing messages. This typically involves a few agents.
MUA, MSA, MTA, MDA, MRA
Okay, maybe not a few but a handful. We won't be diving deep into what each agent does and how it works. Instead, here's a typical flow of an email leaving your server and arriving in your Gmail inbox:
- The MUA or the Mail User Agent creates an email. This could be your PHP script, a shell script, or some program on your server.
- This email is submitted to the MSA or the Mail Submission Agent. This could be a local or remote SMTP server, or a local maildrop service.
- The message is passed on to the MTA or Mail Transfer Agent, which decides how to reach the recipient's domain, as defined by the domain's MX DNS records. This often involves using other, sometimes multiple MTAs to reach the destination. The MTAs are also responsible for handling network failures, rate limiting, retries, and more.
- Once the message arrives at the recipient's domain, the inbound MTA hands it off to their MDA or Mail Delivery Agent, which writes the message to the mailbox store.
- Finally, the recipient's MUA connects to the MRA or Mail Retrieval Agent, typically through POP3 or IMAP protocols, to download the message.
Note that some servers can assume multiple agent roles. In this course we'll focus on outbound email from our server and WordPress applications, so we'll be working with the first three agents: the MUA, MSA, and MTA.
WordPress is our MUA
Our WordPress application is going to act as the MUA. That's where the message is going to be crafted, that's where the recipient is going to be defined. Next, WordPress will hand the message off to the MSA.
wp_mail()
The dreaded wp_mail()
function is responsible for submitting a message. The
function is a wrapper around the PHPMailer library bundled with WordPress.
This library composes the message headers, HTML/text versions, attachments, etc.
Then it calls the PHP mail()
function to finally submit the message.
The mail()
function in PHP is very simple. It takes the message and sends it
as input to a preconfigured binary on the system, defined using the
sendmail_path
configuration option.
On a fresh Ubuntu or Debian system, this binary will not exist, so sending
anything through wp_mail()
will simply fail. However, this binary is provided
by email software packages such as Postfix, Exim, and others. The implementation
may vary, but the input expectation is usually consistent.
Postfix is my recommended option, and sometimes even
comes pre-installed with some hosting providers. The Postfix implementation of
the sendmail
binary uses postdrop,
which simply writes the input message to a local file, without relying on any
services to be up and reachable. This is an extremely efficient and very quick
way to submit a message.
Further processing of this message is handled by Postfix asynchronously.
SMTP plugins
You’ve probably heard of WordPress SMTP and mail plugins. These hijack the
wp_mail()
function or the phpMailer
object to make WordPress connect to an
external sending service and submit the message there.
This is extremely slow compared to writing a file to disk, as it blocks the entire request. It is also prone to failure, as these plugins are susceptible to PHP timeouts, crashes, and memory limits. They usually do not handle retries if the upstream service is unavailable.
However, this method can be helpful in an environment where you have no other option, such as a managed WordPress hosting platform. These environments typically have very strict and locked-down setups for outbound email.
Luckily for us, we have the freedom of a VPS or dedicated server, so as you might have guessed, in this course we are going to go for the local postdrop method using Postfix.
Direct delivery
After a message is dropped off to the MSA, we can attempt to deliver the mail directly to the recipient address. This involves figuring out the MX servers for the recipient's domain and connecting to the recipient's inbound MTA to transmit the message.
This article is for premium members only. One-time payment of $96 unlocks lifetime access to all existing and future content on wpshell.com, and many other perks.