Contents:

Practical course: "DevOps Engineer Specialty"
Learn moreDespite the widespread use of instant messaging apps, billions of users worldwide send emails every day. Behind every email is the Simple Mail Transfer Protocol (SMTP), which ensures the transfer of messages between mail servers. In this article, we will examine the structure of this protocol in detail and offer the opportunity to work with it. This material will be useful for those who are just beginning to be interested in system administration and are looking for a deeper understanding of how email and networks in general function.
Contents
- The Simple Mail Transfer Protocol (SMTP) is a standard method for transmitting email over a network. It is used to send messages from one mail server to another, ensuring their delivery to the recipient. SMTP functions as the primary mechanism that manages the process of sending and routing emails, and also works in conjunction with other protocols, such as POP3 or IMAP, which handle the receiving and storing of email. An important feature of SMTP is its simplicity and efficiency, which makes it widely used in various email systems and applications.
- How secure is it?
- The SMTP protocol, or Simple Mail Transfer Protocol, is the standard mechanism used to send email over the Internet. Its primary function is to transfer messages from client to server and between servers.
When a user sends an email, the email client initiates communication with the SMTP server. This process begins with establishing a TCP connection, typically over port 25, 587, or 465. Once the connection is successful, the client sends SMTP commands, which contain information about the recipient, the sender, and the content of the message itself.
The server processes these commands and, if all data is correct, transmits the email to the recipient's target SMTP server. If this server is currently unavailable, the message may be temporarily stored in a queue for a later delivery attempt.
Once the email reaches the destination server, it is stored until the recipient checks their mail. For this, other protocols, such as POP3 or IMAP, are used, which are responsible for retrieving messages from the mailbox.
The entire process of transmitting emails using SMTP is automated and requires minimal user intervention, while providing a high degree of reliability and efficiency.
- SMTP, or Simple Mail Transfer Protocol, uses various codes and ports to transmit email and handle errors. The most common port for SMTP connections is 25, although ports 587 and 465 are also commonly used.
Status codes returned by the server serve to inform clients about the status of their requests. For example, code 250 indicates successful message transmission, while code 550 signals that the requested address is unavailable or does not exist.
Errors can occur for a variety of reasons, including invalid recipient addresses, network issues, or server settings. Understanding these codes and ports will help users and administrators troubleshoot email issues more effectively.
- How-to: Sending Email Using SMTP, Mailtrap, and Python
SMTP Definition and Purpose
Let's start with the official definition: SMTP is a standardized network protocol used to transmit and route email messages between mail servers and clients. This protocol describes the entire process of a message moving from sender to recipient—from the moment the user clicks the Send button in their email application until the email arrives at the recipient's server.
SMTP, or Simple Mail Transfer Protocol, is designed exclusively for sending email messages, not for receiving them. This can be illustrated by an analogy with traditional mail: the postman delivers a letter to the mailbox, but it remains the recipient's responsibility to retrieve and read it.
The IMAP and POP protocols are used to retrieve emails. When you access Gmail or Yandex Mail, your email client uses one of these protocols to access messages on the server. IMAP, which stands for Internet Message Access Protocol, allows you to work with emails directly on the server and sync them between different devices. Meanwhile, POP, or Post Office Protocol, allows you to download emails from your mailbox to your device. We will tell you more about how these two protocols function in the future.

Its security level
The SMTP protocol was developed at the beginning 1980s. Initially, it was a fairly simple text-based protocol, in which all information was transmitted unprotected, without any encryption. Therefore, if a user entered their authentication credentials, attackers could easily intercept them using network traffic analysis tools.
As the number of email users increased, the SMTP protocol underwent many improvements. Currently, the key characteristics of this protocol are described in two main documents: RFC 5321 and RFC 5322. RFC 5321 covers the technical details of SMTP operation, including the connection process, a list of commands, and the exchange of messages between servers. In turn, RFC 5322 sets standards for the format of the message itself, defining the structure of headers, the organization of content, and other message components.
In 1995, the protocol was supplemented with a new version - ESMTP (Extended Simple Mail Transfer Protocol). This update retained compatibility with the original SMTP and introduced new commands to improve interaction between clients and servers. ESMTP also provided the ability to transmit multimedia messages, including video, images, and attachments. Moreover, the new protocol architecture gave developers the ability to implement additional security mechanisms.
Modern SMTP, unlike its earlier models, operates in conjunction with a variety of other technologies. Specifically, TLS encryption is used to ensure connection security, helping prevent message interception. In addition, mechanisms such as SMTP AUTH, SPF, DKIM and DMARC have been implemented to verify the sender and reduce the level of spam.

Read also:
Information security threats are potential risks that can negatively affect the integrity, confidentiality and availability of data. These threats can take many forms, including malware, phishing, network-based attacks, and physical threats.
To effectively combat these threats, a comprehensive approach is necessary. First and foremost, implement robust security systems, such as antivirus software and firewalls. User training is also key: employees must be aware of potential risks and be able to recognize suspicious activity.
Regular software and security updates will help eliminate vulnerabilities that could be exploited by attackers. In addition, creating data backups and using encryption are important measures that will help minimize the consequences in the event of an incident.
Therefore, systematic work to ensure information security, including technical measures and personnel training, is a prerequisite for protection against threats in this area.
How the SMTP Protocol Works
When the user clicks the "Send" button in the user's mail client, a connection is established to a pre-configured SMTP server. From this moment, the message sending process begins, which includes several key stages: establishing a connection, transferring information, finding the recipient's server, and terminating the session. Let's consider each of these stages in more detail.
The process of establishing an SMTP connection begins with the creation of a TCP connection between the mail client and the server, since SMTP uses TCP as its transport protocol. After a successful connection, the client sends a greeting command to the server, which initiates data exchange and allows the supported extensions to be determined.
The transfer of information is carried out as follows: as soon as the connection is established, the client begins sending SMTP commands to the server. These commands include the addresses of both the sender and recipient, as well as the text of the message itself. On the server side, this process is carried out by the MSA (mail transfer agent) - a program that receives the incoming message, checks it for compliance with established technical standards, and prepares it for subsequent routing.
The search for the server to which the message should be delivered begins with the work of a special program - the MTA (mail transfer agent). This component checks whether the sender and recipient addresses belong to the same domain, for example, @skillbox.ru. If both addresses belong to the same domain, the message is delivered directly within the system, without the need to interact with external resources.
When the domains are different, the server initiates a request to the DNS system to determine the address of the server to which the message should be sent. The sender's SMTP server then establishes a connection with the recipient's SMTP server and transmits the message using a similar algorithm.
Connection Termination. Once the data transfer is complete, the client sends a command to the SMTP server signaling the end of the session. The server acknowledges receipt of this command, after which the TCP connection is terminated.


Read also:
TCP/IP Model: What is it and How does it Work?
System Codes, Errors, and Ports in the SMTP Protocol
To ensure effective interaction between mail servers, the SMTP protocol uses a number of auxiliary mechanisms. These include commands for exchanging data, error codes that help identify problems, and specific ports used to establish a connection. Let's look at each of these components in more detail.
SMTP protocol commands are specific instructions that regulate the process of sending email between the client and the server. They can be viewed as a kind of exchange of remarks between the mail client and the server:
- Sending an email begins with the HELO or EHLO (Hello) command. With this command, the mail client informs the server about itself, providing its domain name or IP address. In response, the server sends a greeting message, confirming the successful connection.
- MAIL FROM. After the sender passes identification, they specify the email address from which the message will be sent. This command marks the start of a new transaction. The server responds by confirming its readiness to accept the email with the 250 OK code.
- RCPT TO (Recipient). At this stage, you must specify to whom the email is addressed. If you need to send a message to several recipients at once, you can use multiple RCPT TO commands. After each command sent, the server will respond with the 250 OK code.
- The DATA command initiates the process of sending the contents of the email message. In response, the server returns the 354 code, signaling its readiness to receive data. The client sends the body of the email, including headers and attachments, and completes the transmission by placing a period on a separate line. In response, the server confirms successful receipt by sending the 250 OK code.
- The QUIT command is intended to terminate the session after the client has sent the message. In response to this command, the server returns the 221 code.
- The RSET (Reset) command is intended to cancel an active transaction. It does not lead to a connection break, but only resets the current state and clears all information about the letter and its recipients.
The interaction model between the mail client and the SMTP server looks like this:

In the image above We illustrate the optimal scenario for data transfer with a guaranteed connection. However, in reality, emails can be rejected, blocked, or encounter various difficulties. As a result, when failures occur, the server receiving the messages generates SMTP error codes. These codes are divided into two categories: temporary and permanent.
The first category of errors is called Persistent Transient Failure. These codes begin with the number 4, followed by two more digits. They typically indicate temporary failures on the mail server. In some situations, retrying the email may result in successful delivery. However, such errors are often used deliberately by servers to delay or filter messages from suspicious senders.
For example, on Yandex servers, the SMTP protocol may return code 421. This occurs when the server is unable to provide a response within the specified timeframe due to high load or various network issues.
The second category of errors is called Permanent Error. Codes in this group begin with the number 5 and signal serious failures, such as a lost SMTP connection or failure to deliver a message. Unlike temporary errors, attempting to resend the message in the event of a permanent error is unlikely to be successful.
Let's consider code 550 in the Yandex SMTP protocol. This server response indicates that the specified recipient is not registered in the system. This may indicate an error in the address, that the mailbox has been deleted, or that the account does not exist at all.

Read also:
HTTP standards: an overview of key aspects of network interactions
Each SMTP server has a unique address and port that are used by the mail client to connect to it. Different email protocols require the use of specific ports. If you think of the internet as a network of buildings around the world, an IP address would be analogous to an "address on a map," while a port could be compared to a specific door through which you enter.
The SMTP protocol originally operated exclusively on port 25. Although this port is still available, it is often blocked on most user networks. This is due to its widespread use by spammers for mass mailings. Additionally, there is port 465, which is rarely used today, as most networks are migrating to more modern encryption methods.
Port 587 is the current standard for transmitting email via the SMTP protocol and is used by many email services. It secures connections using TLS (Transport Layer Security), which encrypts information transmitted over the network.
Numerous email services offer support for port 2525. While this port is not an officially established standard for SMTP, it serves as an alternative in situations where the standard ports 25 or 587 are unavailable or blocked by an Internet service provider.

Sending email via SMTP: using Mailtrap and Python
To safely test the functionality of the SMTP protocol, you should Register for a free Mailtrap account. This service is designed for testing email sending: it simulates the operation of real mail servers, but all sent messages do not reach the recipient's actual inbox, but are stored in a "sandbox" accessible exclusively to you.
After completing registration in your personal account, you must save the copied Username and Password. You will need these credentials to connect to the test SMTP server.

The next step is to install Python, after which you will need to choose an editor for writing code. In this editor, create a new file and paste the following code into it, substituting your data.
After that, open the terminal in the editor and run the script by entering the following command:
When you run the script, a new email should appear in your Mailtrap account. You can open it through the web interface, view the headers and content, and verify that the SMTP transfer was successful.


To learn more exciting information about code, join our channel on Telegram. We will be glad to see you among our subscribers!
Read also:
- OSPF, which stands for Open Shortest Path First, is a routing protocol that is used to exchange route information in large networks. Its main task is to optimize data transmission, providing an efficient and reliable search for the shortest paths for packet transmission.
This protocol operates on the principle of Dijkstra's algorithm, which allows it to quickly determine the most advantageous routes. OSPF divides the network into separate areas, which helps simplify management and improve routing efficiency. Each area has its own routes, and information about them is passed between neighboring routers.
Using OSPF, networks can dynamically adapt to changes, such as equipment failures or topology changes. This makes the protocol especially useful for large enterprises and service providers, where high availability and quick response to network changes are important.
- A server is a specialized computer or system designed to process requests and provide various services to other computers, called clients. The main function of a server is to store, process, and transmit information, providing access to data and resources.
Server operation is based on the client-server model. When a client sends a request, the server receives it, processes it, and returns the necessary data. This can happen in real time, and servers can handle many requests simultaneously.
There are several types of servers, each with its own unique task. For example, web servers are responsible for storing and transmitting web pages, file servers are designed to store and manage files, and mail servers handle email, enabling the sending and receiving of messages. Other types include core servers, game servers, and virtual servers, each serving specific purposes depending on the needs of users and applications.
- The OSI model is a conceptual framework that divides the data transmission process into seven layers. Each of these layers performs its own unique functions and interacts with adjacent layers to form a coherent system. The main goal of the model is to standardize network interactions, allowing various devices and protocols to work together despite differences in their architecture.
The division into layers helps simplify complex network processes, making them more manageable and understandable. Each layer of the OSI model is responsible for specific tasks, from the physical transmission of bits at the bottom layer to providing end-user services at the top. This division promotes more efficient troubleshooting, development of new technologies, and simplified integration processes, since changes at one level do not affect others.
Thus, the OSI model serves as a foundation for understanding and developing networking technologies, allowing developers and engineers to create more compatible and efficient systems.

