📜 ⬆️ ⬇️

Postal Kitchen # 2: SMTP

SMTP (English Simple Mail Transfer Protocol) is a network protocol designed to transfer e-mail over TCP / IP networks.
ESMTP (English Extended SMTP) is a scalable extension of the SMTP protocol. Currently, the "SMTP protocol", as a rule, implies ESMTP and its extensions.

Immediately, I note that at present, SMTP in its pure form is practically not used, because it does not even support elementary authorization ... ESMTP is being used. When / if you send mail with an email client (Outlook, Thunderbird, Evolution, TheBat), this protocol is used to work.

To work using this protocol, you need to connect to the mail server on a specific port and send some sequence of ESMTP commands.
The command is a string of the form
COMMAND [space] parameter (optional)
In response to a command, the server returns a string of the form
XXX [space] ext. information
In this case, XXX number in the server response means:
2XX - command successfully completed
3XX - additional data is expected from the client
4XX - a temporary error, the client must make the next attempt after a while
5XX - fatal error

So, let's get closer to the matter - let's try to send an elementary e-mail from the console through some mail server (it doesn't matter if you have Linux or Windows). So it will be easier to get acquainted with this protocol - immediately in practice. I quote the commands and in parallel explain their meaning.
')
For our experiment, I will use the Yandex mail server. The implication is that there is already an account there ...
Immediately I warn you that after connecting all the commands you need to enter as quickly as possible, because with a delay of about 15 seconds, the connection is automatically terminated. I recommend first to type all commands in advance in a text editor and then simply insert them into the command line.

telnet smtp.yandex.ru 2025 # connect to the smtp mail server. The address and port of the smtp server can be found in the instructions on the site mailer
Answer:
Trying 213.180.204.38 ...
Connected to smtp.yandex.ru.
Escape character is '^]'.
220 Yandex ESMTP (NO UCE) (NO UBE) server ready at Mon, 2 Feb 2009 13:47:22 +0300
Code 220 says successful connection

EHLO [91.198.212.5] # Welcome the server and send it our external IP (you don’t have to send the IP, you can just get by with the EHLO, but the server will most likely curse)
UPD: It is advisable to send not even IP, but a domain name for this IP, like EHLO you.provider.domain without square brackets
Answer:
250-smtp18.yandex.ru Hello 91.198.212.5
250-SIZE 20971520
250-8BITMIME
250-PIPELINING
250-CHUNKING
250-ENHANCEDSTATUSCODES
250 DSN
250-X-RCPTLIMIT 25
250-AUTH = LOGIN
250-AUTH LOGIN
250-STARTTLS
250 HELP
The server accepted the greeting and sent a list of supported commands. From this list we are interested in AUTH LOGIN. This is a command to authorize on the server using a base64-encoded login and password. So, you need to prepare in advance the base64-encoded password and login from your mail. You can do this, for example, here seriyps.ru/crypt or a command in Linux echo [your password / login] | base64

AUTH LOGIN # We inform the server about the intention to pass authorization
Answer:
334 VXNlcm5hbWU6
This very VXNlcm5hbWU6 is the base64-encoded word “Username:”, and the response number 3XX means that the server is waiting for additional information from us. We will not upset him:

YOUR_LOGEN_POCHTY_B_BASE_64 # We are sending your login email to base64, for example dmFzaWFwdXBraW4 =
Answer:
334 UGFzc3dvcmQ6
This is, as you might guess, “Password:” on base64

YOUR_PARRIUM_POCHTY_B_BASE_64 # Send the password of the mail to base64, for example MTIzNDU2
Answer:
235 Authentication successful.
those. authorization was successful. Now you can send e-mail)

MAIL FROM: vasiapupkin@ya.ru # We inform that we want to send mail from the address vasiapupkin@ya.ru The address can be any (including from non-existent domains, however it can be checked when checking for spam)
Answer:
250 2.1.0 Sender syntax Ok;


RCPT TO: billy@microsoft.com # We inform you that we want to send an email to billy@microsoft.com
Answer:
250 2.1.5 Recipient address syntax Ok; rcpt = <billy@microsoft.com>


DATA # Here we inform you that we are starting data transfer.
Answer:
354 Start mail input; end with <CRLF>. <CRLF>
Those. the server will read the data entered in the console until we press Enter Point Enter (after this combination, the letter is sent immediately)

The email consists of the following parts:

We begin to enter the headers of the letter. You can insert a base64-encoded file, but this is a little beyond the scope of the article:
From: Vasia Pupkin <vasiapupkin@ya.ru> # Title for field From
To: Billy G <billy@microsoft.com> # Header for the field To
Subject: Hello Billy # Title for the message subject
(By the way, I want to note that MAIL FROM: vasiapupkin@ya.ru and From: Vasya Pupkin <vasiapupkin@ya.ru> are not at all required to match! Ie, you can send mail from Yandex and pretend that it was sent from mail. ru for example ... What to do is for the protocol for almost 30 years, although it is not very difficult to calculate ...)

Two times Enter, then enter the text of the letter.
Hello, Billy! You'll die tomorrow!
Enter. Enter # We are informing you that you have finished sending the message.
Answer:
250 2.0.0 accepted; S10436885AbZBBKvs
Those. message received for transmission

Now you can send another letter (MAIL FROM: RCPT TO :) or end your session
QUIT # End Session
Answer:
221 2.0.0 smtp18.yandex.ru Out
Connection closed by foreign host.

It's all. As you can see, the protocol is quite simple, the main difficulties are in the formation of the letter body itself.

Summarizing:
telnet smtp.yandex.ru 2025
EHLO 91.198.212.5
AUTH LOGIN
____BASE_64
____BASE_64
MAIL FROM: vasiapupkin@ya.ru
RCPT TO: billy@microsoft.com
DATA
From: <vasiapupkin@ya.ru>
To: <billy@microsoft.com>
Subject: Hello Billy
Hello, Billy! You will be die tomorrow!
.
QUIT


Of course, there is no information on sending mail in non-ASCII text encodings, not written about the attached files and MIME, but if you need details, here are some links:
Wiki e-mail
SMTP Wiki
MIME Wiki
rfc5321

When developing applications directly with SMTP, it is usually not necessary to work, for this they use various frameworks or standard functions. For PHP you can see:
SMTP PEAR extension
PHPMailer library for working with e-mail
Successful experiments!

Source: https://habr.com/ru/post/51772/


All Articles