Recently, while reading upon mailboxes I was intrigued about the SMTP protocol, and tried to understand how it works.
The Simple Mail Transfer Protocol
The Simple Mail Transfer protocol is responsible for sending email from a sender’s email address to a recepient, by connecting through multiple MTAs in order to find the right mailbox to transfer email to.
Since SMTP is a client-based protocol, it uses DNS to find the right IP to send emails to.
Playing around with a simple SMTP Server
We can use the following python command to create a simple SMTP server on port 2525.
$ sudo python3 -m smtpd -n -c DebuggingServer localhost:2525The command doesn’t return any output once the server is started.
Now we use telnet to connect to the SMTP server using the command:
$ telnet localhost 2525Trying ::1...Connected to localhost.Escape character is '^]'.220 pop-os.localdomain Python SMTP proxy version 0.3HELO shrirambalaji.dev250 pop-os.localdomainMAIL FROM: hello@shrirambalaji.dev250 OKRCPT TO: shrirambalaji1996@gmail.com250 OKDATA354 End data with <CR><LF>.<CR><LF>Hello World from SMTP!.250 OK- The
HELOcommand is responsible for letting the SMTP server know that an email message is incoming - The
MAIL FROMcommand mentions the sender’s mail address. - The
RCPT TOcommand mentions the receiver’s mail address. - The
DATAcommand refers to ASCII encoded data, that is going to follow it and needs to be terminated by an empty line and a .(dot) which is what theEnd data with <CR><LF>.<CR><LF>is. - Similar to HTTP, 2XX status codes are positive 3xx codes are positive intermediate, 4XX are negative.
- Particularly
220refers to<domain> Service Ready,250refers torequested mail action okay, completed. The list of all status codes can be found here.