Day 23 — CS Fundamentals December — Mail Transfer — Delving into SMTP

Yashvardhan Kukreja
4 min readDec 24, 2019

In yesterday’s article, I introduced mail transfer and explained the top-view of the overflow of mail transmission happening over the internet.

But the real magic of reliability for mail transmission is performed by SMTP i.e. Simple Mail Transfer Protocol.

And, needless to say, for a software engineer, especially a network engineer, it is important to understand comprehensively about SMTP.

So, today’s article is going to be about that!

Let’s dive in!

Introduction

So, you might remember about MTA client and MTA server from the previous yesterday’s article.

But in the previous article, I just abstracted them as some agents for doing some tasks.

But, in this article, we will deep dive into MTA Client and MTA Server.

So, what is SMTP?

Simple Mail Transfer Protocol is the protocol that defines MTA Client and MTA Server in the internet.

So, basically, SMTP is more or less about MTA Client communicating with the MTA Server.

And it communicates in the form of commands and responses.

MTA Client sends a command to the MTA Server.

And, MTA Server sends a response to the MTA Client for that command.

So, I’ll now list and briefly explain all the commands which are supported by SMTP to be sent from MTA Client to the MTA Server.

Commands which MTA Client can send

  • HELO: This command is used by client to identify itself. By clients, I mean e-mail domains like gmail.com, yahoo.com, etc. So, the command would look like
HELO: gmail.com
  • MAIL FROM: This command is used to identify the sender (user). So, the command would look like
MAIL FROM: yash1999@gmail.com
  • RCPT TO: This command is used to identify the recipient of the mail. So, the command would look like
RCPT TO: myfriend@yahoo.com
  • DATA: This command involves the actual mail body with it. So, the command would look like
DATA:
This is the mail body..
yada yada yada...!!!
  • QUIT: This command is straight away sent to terminate the mail message.
  • RSET: This command is straight away sent to reset the connection.
  • VRFY: This command is used to verify if the email address of the recipient exists or not.
VRFY: myfriend@yahoo.com
  • HELP: This command is used get information about other commands from the recipient mail server. So, the command would look like
HELP: VRFY

Responses which MTA Server can send

There are three types of responses that MTA Server can send back to the MTA Client:

  • Positive Condition Response: This type of response includes positive informative responses like “System status”, “Service Ready”, “Service Terminating”, etc.
  • Transient Negative Completion Response: This type of response includes negative one-time or temporary responses like “Service Not Available”, “Mailbox Not Available”, etc.
  • Permanent Negative Completion Response: This type of response includes negative responses which needed to be fixed from backend, like “Transaction Failed”, “Syntax Error”, etc.

Let’s talk about Mail Transfer Phases

So, there are three mail transfer phases:

Phase 1). Connection Establishment

This phase is for setting up the connection between the MTA Client and MTA Server.

So, it is performed in the following steps:

  • MTA Server sends a “220 Service Ready” response to the MTA Client.
  • Then, MTA Client sends the HELO command with its domain name to verify the domain for connection with the MTA Server.
  • MTA Server on receiving it sends “250 OK” response and this sets up the connection between the MTA Client and MTA Server.

Phase 2). Mail Transfer

This phase involves sending the entire mail from MTA Client to MTA Server

So, it is performed in the following steps:

  • So, MTA Client sends different parts of the mail in sequential order.
  • So, it starts with sending the MAIL FROM command which contains sender’s address.
  • MTA Server responds to it with “250 OK”.
  • Then, MTA Client sends RCPT TO command which contains the recipient’s address.
  • MTA Server responds to it with “250 OK”.
  • Then, MTA Client sends DATA command which contains the mail’s header and body.
  • MTA Server responds to it with “250 OK”.
  • And hence, this leads to a successful transfer of the mail.

Phase 3). Connection Termination

This phase is performed so as to terminate and free-up the connection between the MTA Client and MTA Server.

So, it is performed in the following steps:

  • MTA Client sends a QUIT command to the MTA Server.
  • The MTA Server responds with “221 Service Closed” response to indicate that the connection/service has been closed successfully.
  • And hence, the connection between the MTA Client and MTA Server gets terminated.

That’s it!

Thanks a lot for reaching till here :)

I hope you understood this article along with the previous article and hence, you got a deep idea of mail transfer happening over the internet.

Stay tuned for another article which is going to come tomorrow associated with some other interesting CS fundamental.

LinkedInhttps://www.linkedin.com/in/yashvardhan-kukreja-607b24142/

GitHubhttps://www.github.com/yashvardhan-kukreja

Email — yash.kukreja.98@gmail.com

Adios!

--

--

Yashvardhan Kukreja

Software Engineer @ Red Hat | Masters @ University of Waterloo | Contributing to Openshift Backend and cloud-native OSS