Site icon Mailtrap

Understanding SMTP – The Protocol Behind Email Delivery 

This is a cover image for an article that explains SMTP Protocol in detail

SMTP is a fundamental protocol used in email communication. Most likely, it’s behind the emails we send and receive each day. Though it’s a simple protocol, it still involves multiple components and a lot of details. 

In this blog post, we’ll take a closer look at SMTP and explore its core features and functionalities. 

What is SMTP? 

SMTP or Simple Mail Transfer Protocol is an application layer protocol that makes it possible to transfer emails between different servers and computer networks. It does this by defining the rules of communication.

The original model was introduced in 1982. According to RFC 821, the user creates the connection request. In response, the sender-SMTP initiates a two-way connection with the receiver-SMTP. In modern terms, these are SMTP client and SMTP server, respectively. SMTP client and SMTP server communicate with commands and responses (more on that later), similar to real-life conversations.

The RFC 821 also defined the model for SMTP use. You can see the scheme in the illustration below. 

Once the connection is established, the SMTP client will transfer headers, recipients, body messages (including attachments), and all the data to the SMTP server step-by-step. When the transmission is complete, the connection will be closed. 

By definition, the full form of SMTP is when SMTP clients support relaying, email queues, and alternate address functions. This is called a fully-capable SMTP. If these functions aren’t supported, the SMTP isn’t fully-capable. In that case, relevant RFCs recommend using the message submission protocol instead. 

Keep in mind that SMTP can only send simple messages i.e., plain text without attachments. We use a separate protocol, Multipurpose Internet Mail Extensions, MIME (RFC 2045), to send attachments, message bodies exceeding the character limits imposed by SMTP, messages in languages other than English, and HTML/CSS formats. 

MIME is an extension protocol – it enhances the capabilities of SMTP, but it doesn’t operate separately. Most modern email services support MIME. 

What is ESMTP?

ESMTP or Extended Simple Mail Transfer Protocol was first introduced in 1995 in the RFC 1869. The purpose was to create a unified structure for all future extensions. The extensions aim to add functionalities that SMTP lacks by default. RFC 5321 consolidated and obsoleted previous documents. 

ESMTP uses a new EHLO command to initiate the connection. It also allows the use of additional parameters in SMTP’s MAIL FROM and RCPT TO commands. As a result, ESMTP removes the 512-character limit for additional parameters and leaves it only for cases when additional parameters aren’t defined. 

Both ESMTP and SMTP are widely used today. If the EHLO command isn’t supported, the connection should fall back to the SMTP and its HELO command. 

Speaking of extensions, we should also mention the SMTP-AUTH extension, which adds an authentication step to the process. This means that the mail client should log in to the mail server using its username and password. While SMTP authentication can’t protect against spoofing, it is an important security measure. 

What is SMTPS? 

SMTPS or Simple Mail Transfer Protocol Secure is a method that secures SMTP with the help of transport layer security (TLS) or Secure Sockets Layer (SSL) protocols. These security layers encrypt messages to prevent spammers or spoofers from viewing the contents of the emails. 

Though SSL is still widely used, TLS (more precisely, its 1.3 version) is considered to be the safest protocol for email encryption. For more information on SMTP security, read our dedicated blog post

Types of SMTP

RFC 5321 differentiates between four types of SMTP systems:

SMTP infrastructure 

As we saw above, the main components of the SMTP model are User, Sender-SMTP (SMTP client), and Receiver-SMTP (SMTP server). However, mail agents also participate in the process of sending and receiving emails. Moreover, the SMTP model includes SMTP relay in specific scenarios. Let’s see what each of them means and what function they have. 

SMTP server 

SMTP server is an application for sending emails. It receives electronic messages from email clients (Gmail, Yahoo! Apple Mail, AOL, etc.) and transfers them to other servers. These can be other SMTP servers or an incoming mail server. 

SMTP email server can be local or cloud-based. A local SMTP server is a good option for those who don’t want to depend on third-party servers. On the other hand, a cloud-hosted SMTP server requires less effort and can be more secure in the majority of cases. 

Mail Agents 

There are four main agents: 

Note that the lines between agents’ responsibilities can be blurry at times, yet they are still useful for illustrative purposes. In the real world, MUAs, MTAs, and SMTP servers are the most essential components of email delivery. 

SMTP relay 

SMTP relay is the process of transmitting emails between SMTP servers hosted on different domains (from @gmail.com to @yahoo.com, for example). 

MTAs check if the domain names are the same. If they are, the SMTP relay won’t happen. But if the domains aren’t the same, MTAs will query Domain Name System (DNS) records to find the IP address for the recipient’s domain. Once the IP is located, they will route the message between one or multiple MTAs (SMTP relay) until it’s finally delivered to the recipient’s SMTP server. 

How does SMTP work?

An SMTP session begins when the client opens a Transmission Control Protocol connection (TCP connection, sometimes called TCP/IP) to the SMTP server. The server responds with an opening message, expressed with code 250. This process is often called an SMTP handshake. 

The client then sends a HELO command (EHLO for ESMTP) and identifies itself. It often follows up the command with the domain name or IP address. In non-technical words, the client says, “Hello, my name is John the Client, I’m sending an email from gmail.com, and my IP is 192.0.2.0”. The server will respond with code 250 again. 

After that, the email transfer stage will begin. Email contents will be transferred step-by-step with MAIL FROM (john@gmail.com), RCPT TO (oliver@gmail.com), and DATA (‘Hey, how have you been?’) commands. If the server accepts the transaction, the client will transfer email headers. It’s necessary to use an end-of-line indicator (period ‘.’) once everything has been transmitted. 

The server will respond with code 250 if the transaction is successful. The client will initiate the termination of the SMTP connection with the QUIT command, and the server will close the transmission channel with code 221. 

This is, of course, a simplified example without an SMTP relay, forwarding, gateways, or error codes. For more information on these, please refer to RFC 5321

What about the SMTP queue? 

SMTP queue is a set of emails that are waiting to be delivered. Emails are usually put in the queue when receiving SMTP server isn’t ready to accept emails, or you’re sending large volumes at once. When the server responds, the emails will be delivered one by one. SMTP queue is a sort of buffer between you and the receiving server. 

SMTP commands and responses

We mentioned some of the SMTP commands and responses in the example above. But, while sending emails with SMTP, you may encounter other commands and responses. 

In general, commands are alphabetic character strings ending with <CRLF>. If additional parameters follow, the characters will be terminated by <SP>. 

Responses (or replies) are numeric completion codes that can be positive or negative. Typically, they are followed by a text string. 

Commands and responses are composed of an ASCII character set. 

Commands 

Syntax: "EHLO" SP ( Domain / address-literal ) CRLF or"HELO" SP Domain CRLF

Syntax: MAIL FROM:<reverse-path> [SP <mail-parameters> ] <CRLF>

Syntax: RCPT TO:<forward-path> [ SP <rcpt-parameters> ] <CRLF>

Syntax:  "DATA" CRLF

Syntax: "VRFY" SP String CRLF

Syntax: "EXPN" SP String CRLF

Syntax: "NOOP" [ SP String ] CRLF

Syntax: "QUIT" CRLF

Syntax: "HELP" [ SP String ] CRLF

Syntax: "RSET" CRLF

SMTP protocol may also support some ESMTP commands, such as STARTTLS, AUTH, and others. 

Responses 

Common positive responses 

Negative responses or error codes

SMTP ports

SMTP ports are communication endpoints that help identify the exact location of internet addresses. The most common ports used with SMTP are 25, 465, 587, and 2525. 

Comparing SMTP, IMAP, and POP3

Apart from SMTP, the most common email protocols are IMAP and POP3. SMTP is an outbound email server used for sending and delivering emails. IMAP and POP3 are message access protocols used for retrieving incoming messages from the email server. 

IMAP is an Internet Message Access Protocol that connects to the server and downloads messages on request. The messages won’t be deleted once the connection is terminated. With IMAP, users can access their emails from any computer or device. 

POP3 or Post Office Protocol 3 also connects to the server, but it downloads all the received messages. Once that’s completed, it will delete all the emails from the server. Unlike IMAP, POP3 is device-dependent. 

More information on the differences between these protocols is available here as well as in our dedicated video.

How to send emails with SMTP 

One of the main benefits of SMTP is that it makes it easy to send emails from different apps or devices. Most programming languages allow users to route outgoing emails through SMTP either natively or with the help of different libraries. Unlike Web API (or Email API), SMTP integration doesn’t require advanced coding skills. 

You’ll need working SMTP credentials (such as host, port, username, password, and encryption) and the relevant programming language to complete that task. You can check out our recent blog post for more details on that. 

SMTP as a service – testing and sending 

SMTP (or, more precisely, SMTP server) is provided to users as a service for two main purposes – testing and sending. 

In terms of testing, SMTP service implies a fake SMTP server that captures outgoing mail. That way, emails don’t reach real inboxes, creating a safe environment for testing. 

When it comes to email sending, SMTP service is synonymous with SMTP relay service. Email providers have their own SMTP servers that enable users to send marketing or transactional emails. 

Ideally, these two SMTP services can be combined into one robust tool such as Mailtrap

Mailtrap is an Email Delivery Platform that combines Email Testing and Email Sending. It’s a complete email infrastructure that allows users to test SMTP server, send emails with it, and monitor their performance with actionable analytics. 

Email Testing is a Sandbox that captures all the SMTP traffic in a virtual inbox and eliminates the chances of spamming users. You can easily inspect and debug your emails in staging with HTML/CSS or spam checks, view tech info, or use manual/automatic forward features. Email Testing provides multiple inboxes for different projects and product stages. 

Email Sending is an infrastructure with high email deliverability rates by design. With its Email API and SMTP service, the integration is smooth and effortless. All you need to do is verify your domain and choose the preferred method of integration. Once that’s done, you can reach recipients’ inboxes in a matter of seconds. 

Email Sending also has actionable in-depth analytics with drill-down reports and helicopter-view dashboards that keep you on top of your deliverability metrics. 

Exit mobile version