What is SMTP and How it Works?

Posted on Apr 10, 2020 12:56:53 PM | Last Update on Apr 10, 2020 12:58:11 PM

Everyone of us are using email to send message to someone. But have you thought that, how this email work.

In this post we will discuss and answer the below questions.

  • What is SMTP?

  • How does the SMTP works?

  • Is there any Standard use for SMTP?

What is SMTP?

(pronounced as separate letters) Short for Simple Mail Transfer Protocol, a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP. In addition, SMTP is generally used to send messages from a mail client to a mail server. This is why you need to specify both the POP or IMAP server and the SMTP server when you configure your e-mail application.

source: https://www.webopedia.com/TERM/S/SMTP.html

How does the SMTP Works

SMTP works when someone send an email to another recipient it will send it to the SMTP server of that recipient.

The diagram below is the very simple way on how does SMTP send you email to another email.

SMTP-flow

Let say we want to send an email to [email protected] and the sender email will be [email protected]

  1. The user will send an email going to SMTP server, but ofcourse it will go first to your network and internet.
  2. From your network you it will pass the message to SMTP of coderstoolbox.dev and established a socket connection
    • Heres the response and request of sender client and smpt server
      • Before checking the response and request here, the guideline
        • C means client (Client)
        • S means server (SMTP Server)
        • EHLO - Hello to start handshake
        • MAIL FROM - Sender email address
        • RCPT TO - The recipient of the message
        • DATA - The content of the message with headers
Resolved smtp.coderstoolbox.dev as << smtp ip address >> [cache miss]
Connection established to << smtp ip address >>
S: 220 <<client pc>> ESMTP
C: EHLO [<< smtp ip address >>]
S: 250-smtp.coderstoolbox.dev Nice to meet you, [<< client ip address >>]
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SMTPUTF8
S: 250-AUTH LOGIN PLAIN
S: 250 STARTTLS
C: STARTTLS
S: 220 Ready to start TLS
Connection upgraded with STARTTLS
C: EHLO [<< smtp ip address >>]
S: 250-smtp.coderstoolbox.dev Nice to meet you, [<< client ip address >>]
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SMTPUTF8
S: 250 AUTH LOGIN PLAIN
SMTP handshake finished
C: AUTH PLAIN AGFiYwBkZWY=
S: 235 Authentication successful
User "abc" authenticated
Sending message <[email protected]> to <[email protected]>
C: MAIL FROM:<[email protected]>
S: 250 Accepted
C: RCPT TO:<[email protected]>
S: 250 Accepted
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
Content-Type: multipart/alternative;
 boundary="--_NmP-f977e9e906cd1548-Part_1"
From: =?UTF-8?Q?Fred_Foo_=F0=9F=91=BB?= <[email protected]>
To: [email protected]
Subject: =?UTF-8?Q?Hello_=E2=9C=94?=
Message-ID: <[email protected]>
Date: Wed, 08 Apr 2020 05:15:35 +0000
MIME-Version: 1.0

----_NmP-f977e9e906cd1548-Part_1
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello world?
----_NmP-f977e9e906cd1548-Part_1
Content-Type: text/html
Content-Transfer-Encoding: 7bit

<b>Hello world?</b>
----_NmP-f977e9e906cd1548-Part_1--
.
<622 bytes encoded mime message (source size 619 bytes)>
S: 250 OK: message queued
Closing connection to the server using "end"
  1. When the Sender SMTP recieve an email it will save it to POP3/IMAP Server as a sent message
  2. After saving, the sender SMTP will request all list of MX records from DNS for coderstoolbox.online
  3. The DNS will response all mx records with priority.
  4. The SMPT will try to connect to the given address by the dns.
    • The lesser the priority will be the first to test to connect
    • If the connection failed it will try the other address.
    • If successful connection, both will talk to each other
    • If you will notice both are connected to each other, in this part the sender smtp will be a client of reciever smtp
    • Here are the sample message and response of both server
      • Before checking the response and request here, the guideline
      • C means client (Sender SMTP Server)
      • S means server (Reciever SMTP Server)
      • EHLO - Hello to start handshake
      • MAIL FROM - Sender email address
      • RCPT TO - The recipient of the message
      • DATA - The content of the message with headers
Resolved smtp.coderstoolbox.online as << receiver ip address >> [cache miss] // resolving the smpt address of reciever
Connection established to << receiver ip address >>
S: 220 smtp.coderstoolbox.online ESMTP // Response success
C: EHLO [<< sender ip address >>] // Tell hello to reciever
S: 250-smtp.coderstoolbox.online Nice to meet you, [<< sender ip address >>] // Response to accept the request
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SMTPUTF8
S: 250-STARTTLS
S: 250 SIZE 26214400
C: STARTTLS // Tell to start tls
S: 220 Ready to start TLS
Connection upgraded with STARTTLS
C: EHLO [<< receiver ip address >>]
S: 250-smtp.coderstoolbox.online Nice to meet you, [<< sender ip address >>]
S: 250-PIPELINING
S: 250-8BITMIME
S: 250-SMTPUTF8
S: 250 SIZE 26214400
SMTP handshake finished

Sending message <[email protected]> to <[email protected]>
C: MAIL FROM:<[email protected]> // Tell Receive SMPT to that the message is from the email [email protected]
S: 250 Accepted
C: RCPT TO:<[email protected]> // Tell Receiver SMPT the recipient will be [email protected]
S: 250 Accepted
C: DATA // Tell to Receiver that this is the raw message that will be send.
S: 354 End data with <CR><LF>.<CR><LF>
Content-Type: multipart/alternative;
 boundary="--_NmP-f977e9e906cd1548-Part_1"
From: =?UTF-8?Q?Fred_Foo_=F0=9F=91=BB?= <[email protected]>
To: [email protected]
Subject: =?UTF-8?Q?Hello_=E2=9C=94?=
Message-ID: <[email protected]>
Date: Wed, 08 Apr 2020 05:15:35 +0000
MIME-Version: 1.0,

----_NmP-f977e9e906cd1548-Part_1
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello world?
----_NmP-f977e9e906cd1548-Part_1
Content-Type: text/html
Content-Transfer-Encoding: 7bit

<b>Hello world?</b>
----_NmP-f977e9e906cd1548-Part_1--
.
<623 bytes encoded mime message (source size 620 bytes)>
S: 250 OK: message queued
Closing connection to the server using "end"
Connection closed
  1. Receiver SMTP will save the message to its own POP3 or IMAP Server
  2. Then lastly the email will sync by the receiver client from POP3 or IMAP Server

If you notice the whole flow, the sender smpt will be another client so that it can send to smpt of the reciever. Means, that the smpt server will also be a client to deliver the email to its real reciever.

Do we have any standard that was already exists?

Yes we have, this must be followed by all the email providers.

Here are some list of RFCs for SMTP.

Some RFC for smtp are already obesele, this two is the most updated as of now

Conclusion

We have learn about SMTP and how it works. SMTP means Simple Mail Transfer Protocol, use for exchanging message. When sending email, the client will first connect to its own SMTP server and they will exchange messages. After the SMTP server receive the necessary data, the SMTP server will save the message to POP3/IMAP server. The SMTP Server will resolve the Recipient SMTP server using the MX record from DNS provider. After that the SMTP Server will call connect to recipient/receiver server, in this time the sender SMTP server will became a client of the Receiver SMTP Server. From Receiver SMTP it will save the message to its own POP3/IMAP Server. And the Receiver client will sync the message to its own client. Hope you have learned something in this post.

Thank you and happy coding

© 2020 Coders Toolbox Online. All rights reserved.

Made with by Making Creations