Deliverability
Why do we need SMTP commands and what do they do? Well, we use SMTP (Simple Mail Transfer Protocol) commands to communicate with email servers. These commands are essential for initiating and managing the process of sending emails. By issuing commands like “EHLO” (which initiates the connection), “MAIL FROM” (specifying the sender’s email address), and others, we can instruct email servers on how to handle the emails we want to send. Without these commands, the email server wouldn’t know how to process and deliver our messages.
Before an email even hits the inbox, there’s a lot happening behind the scenes. It starts with TLS (Transport Layer Security), which kicks in to encrypt the connection and keep your data safe from prying eyes. Then the SMTP protocol takes the wheel, laying out the rules for how that message gets delivered. Every message also carries a header—basically the who, what, and when of your email. Want to see the process in action? Fire up your command line. With just a few SMTP commands, you can walk through the entire journey from sender to server and spot deliverability issues before they ever become a problem.
So, what is SMTP? Simple Mail Transfer Protocol (SMTP) is just a set of rules that handles email sending, receiving, and relaying.
Here’s a breakdown:
To complete the email sending-and-receiving process, your recipient’s email client uses IMAP (another protocol) to download messages from the receiving IMAP email server when your subscriber decides to check their email. IMAP helps manage emails so your recipients can interact with them in their mailbox UI.
Hang in there, just one more acronym before we get into commands. ESMTP (Extended Simple Mail Transfer Protocol) extends the capabilities of SMTP to allow sharing of multimedia files, like pictures, audio files, and videos. ESMTP includes additional commands allowing the following functionalities:
Below, we’ll go over both ESMTP and SMTP commands.
An SMTP command is a specific instruction sent from an email client to a mail server that tells it what action to perform—like sending a message, specifying a sender or recipient, or starting a secure connection.
Yes: If you’re an email developer, you need to know SMTP commands. But if you’re a regular Gmail account user, you can probably save your brain space for random Game of Thrones trivia.
SMTP commands are used in a particular sequence to facilitate communication between two servers to deliver an email. Here are a few scenarios in which you might want to know SMTP commands:
If any of these use cases sound familiar, read on!
Think of an SMTP session as a dialogue between your client and an SMTP server. You send out commands with alphabetical characters, and the server responds with numeric codes.
What’s a client, you ask? Clients can be your email client, like Gmail, which issues commands on your behalf. Or they can be a terminal that you use to issue commands.
Usually, SMTP sessions follow a standard sequence:
Now that we know how an SMTP session looks, let’s go over some essential SMTP and ESMTP commands. We’ve listed these in the sequence in which they might be used:
The first command you’ll need to know is the HELO command. HELO starts the SMTP session and allows the client to greet the server and introduce itself. The command takes the domain name or IP address of the SMTP client as an argument, as shown below.
HELO mailgun.net
The HELO command is mostly considered obsolete, but SMTP clients and mechanisms are required to support it as a fallback for conversing with older implementations.
Remember how we said above that ESMTP extended SMTP? As such, ESMTP sometimes has different commands from SMTP. EHLO is the ESMTP alternative to SMTP HELO for servers that support ESMTP. The SMTP server will respond with an SMTP error code if it doesn’t support ESMTP. In this case, you’ll have to start the handshake process all over again. Check out the sample ESMTP command below.
EHLO mailgun.net
The AUTH command is an ESMTP command that authenticates the SMTP client to the SMTP server. It can take different keywords, depending on the level of security and the login method: PLAIN, LOGIN, and CRAM-MD5. Your session is authenticated when the server provides a positive response. Check out the AUTH command below.
AUTH CRAM-MD5
Learn more about AUTH command keywords.
After you’ve initiated the client-server handshake with a HELO or EHLO command, you can use the MAIL FROM command to start a mail transfer. Specifically, you (as the sender) state the sender’s email address in the “from” field. The MAIL FROM command takes the “from” email address as an argument, as shown in the SMTP example code below.
MAIL FROM "test@mailgun.net"
Use the RCPT TO command to specify the recipient as an argument. If you have more than one recipient, use RCPT TO to specify each recipient separately. Check out the sample RCPT TO command below.
RCPT TO "subscriber@recipientsinbox.com"
The DATA command is how your SMTP client asks the SMTP server for permission to transfer your email message (the data). Once the server has responded with a 354 code to grant permission, the client delivers your email content line-by-line. Use a final line with a period (“.”) to terminate the data transfer.
Check out the example DATA command below.
DATA
354 (server response code)
Date: Wed, 30 July 2022 06:04:34
From: test@mailgun.net
Subject: SMTP session tutorial
To: subscriber@recipientsinbox.com
Body text
.
The BDAT command is the ESMTP version of the DATA command that transfers email content. BDAT takes two arguments:
Check out the sample BDAT command below.
BDAT 67 LAST
To: subscriber@recipientsinbox.com
From: test@mailgun.net
Subject: SMTP session tutorial
250 Message OK, 67 octets received
BDAT is used in the Microsoft Exchange Server.
Issue the VRFY command to ask the server to confirm that a particular username or mailbox exists on the localhost. Note that some SMTP email servers ignore the VRFY command since it can be a security loophole that hackers exploit to lift email addresses. Take a look at a sample VRFY command below.
VRFY user2
250 Jada Jonesjadaj@mailgun.net
Ready to reset your SMTP connection to the initial state without severing your connection? Use the RSET command. This command takes no arguments. Check out the sample command below.
RSET
Okay, let’s say you’re finished with your SMTP session. You want to clear the data and terminate your session. Simply use the QUIT command. Once the server responds with a 221 code, your client will close the connection. Check out the sample QUIT command below.
QUIT
This is probably the most useful command. Need help but don’t know where to turn? Try the HELP command first. This command pulls up a list of possible commands that the SMTP server supports. You can supply specific commands as an argument to the HELP command to get help with specific commands. Check out the example HELP command below.
HELP
If SMTP commands are how you communicate with an SMTP server via an email client, how does the SMTP server talk to you? That’s easy: through SMTP response codes.
SMTP response codes are three-digit numeric sequences. Each digit tells you something different. Consider a response code of 250:
As you can imagine, the first digit is the most important because it gives you an idea about whether or not your command worked. Check out the following table from RFC 5321 (Simple Mail Transfer Protocol) that lays out what various first digits mean:
Reply code | Meaning |
---|---|
2.y.z | The command that was sent was successfully completed on the remote server. The remote server is ready for the next command. |
3.y.z | The command was accepted, but the remote server needs more information before the operation can be completed. The sending server needs to send a new command with the needed information. |
4.y.z | The command wasn’t accepted by the remote server for the reason that might be temporary. The sending server should try to connect again later to see if the remote server can successfully accept the command. The sending server will continue to retry the connection until either a successful connection is completed (indicated by a 2.y.z code) or fails permanently (indicated by a 5.y.z code). An example of a temporary SMTP error is low storage space on the remote server. Once more space is made available, the remote server should be able to successfully accept the command. |
5.y.z | The command wasn’t accepted by the remote server for the reason that isn’t recoverable. The sending server won’t retry the connection and will send a non-delivery report back to the user who sent the message. An example of an unrecoverable error is a message that’s sent to an email address that doesn’t exist. |
And that’s it! We know, super light and easy info. Whether you’re looking for an SMTP server solution to power your email sending, or if you’re looking for details on exactly what SMTP port to use (25? 587? 2525?), Mailgun’s got you covered.
As always, don’t hesitate to reach out. Let us help you get sending today.