Deliverability
What are SMTP commands and what do you need to know about them?
This article will go over what SMTP commands are and how you can get in touch with your SMTP server. Then, we’ll leave you with some essential SMTP commands and response codes.
PUBLISHED ON
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.
Table of contents
1. The HELO or EHLO command
2. The AUTH command
3. The MAIL FROM command
4. The RCPT TO command
5. The DATA command
6. The BDAT command
7. The VRFY command
8. The RSET command
9. The QUIT command
10. The HELP command
What is SMTP?
Before we get into the specifics of commands, let’s start with the basics. What is SMTP? Simple Mail Transfer Protocol (SMTP) is just a set of rules that handles email sending, receiving, and relaying.
Here’s how it works:
The SMTP method establishes a connection between your (the sender’s) email client and your SMTP server.
The SMTP server performs a series of verifications.
The SMTP server relies on SMTP to ferry your email to your recipient’s inbox provider, like Microsoft Outlook or Gmail.
Then SMTP severs the connection between your email client and the SMTP server.
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.
Want to know more about SMTP and how it works? If you’re an email geek like us, the answer is yes. Check out our post on what is SMTP and how does it work to learn more.
What is ESMTP?
Hange 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:
Sender authentication
Encrypting emails with SSL
Attaching multimedia files
Restricting email size based on server specifications
Simultaneous transmission to several recipients
Below, we’ll go over both ESMTP and SMTP commands.
Do I even need to know SMTP commands?
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:
You want to configure SMTP on your computer or network
You need to troubleshoot your email service
You need to contact your SMTP server
Okay, this one’s pretty specific, but what if you want to access email in the middle of a Telnet session? Telnet allows you to virtually access a computer and implement remote, collaborative two-way access
Also specific: you’re trying to send emails from inside a Linux shell
You want to run an SMTP test
If any of these use cases sound familiar, read on!
How should an SMTP session flow?
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:
Handshake: The client and the server establish a Transmission Control Protocol (TCP) connection
Email transfer: The client passes your email message to the SMTP server, and the server performs authentication checks
Termination: The client and the server close the TCP connection
What are some essential SMTP commands?
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 HELO or ELHO command
The AUTH command
The MAIL FROM command
The RCPT TO command
The DATA command
The BDAT command
The VRFY command
The RSET command
The QUIT command
The HELP command
1. The HELO or EHLO command
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.
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.
2. The AUTH command
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.
Learn more about AUTH command keywords.
3. The MAIL FROM command
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.
4. The RCPT TO command
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.
5. The DATA command
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.
6. The BDAT command
The BDAT command is the ESMTP version of the DATA command that transfers email content. BDAT takes two arguments:
The length of the data chunk (in octets)
An indicator that the data chunk is terminating
Check out the sample BDAT command below.
BDAT is used in the Microsoft Exchange Server.
7. The VRFY command
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.
8. The RSET command
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.
9. The QUIT command
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.
10. The HELP command
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.
What are the SMTP response codes?
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:
The first digit (“2”) indicates if your command was good, bad, or incomplete
The second digit (“5”) tells you what kind of response was sent
The third digit (“0”) gives additional information about your command
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 |
---|---|
Reply code | |
2.y.z | The command that was sent was successfully completed on the remote server. The remote server is ready for the next command. |
Meaning | |
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. |
Wrapping up
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.
Learn more about SMTP with Sinch Mailgun
Free SMTP relay service
Particularly for growing businesses, you want access to powerful features, real-time analytics, and the ability to scale as both your email marketing and outgoing mail multiplies. That flexibility is at the core of Mailgun so as your business and marketing campaigns evolve, your free SMTP service doesn’t have to change.