Sending Email Through SMTP Server Using Python


Sending emails is a vital functionality in today’s software applications, and Python excels in this area with its robust libraries such as smtplib and email. This guide aims to simplify the process of email dispatch using Python, focusing on the integration with Yandex’s SMTP server. Whether you’re automating email notifications or building a system that requires email capabilities, this guide will provide you with a clear and concise approach to harnessing Python’s email-sending potential with Yandex.

SMTP Server Settings

To utilize Python for sending emails, the first crucial step is to understand and configure the settings for your email server. In this case, we’re focusing on Yandex, a popular email service provider. Here’s what you need to know about Yandex’s SMTP server settings:

  • SMTP Server – This is the domain through which your emails will be routed.
  • Port Number587 – This port is typically used for email sending with TLS encryption, ensuring secure email transmission.
  • Authentication Credentials: Your Yandex email account’s username and password. These credentials are essential for establishing a secure connection to the Yandex SMTP server.
Python email automation Yandex
Yandex SMTP Python tutorial
Automating emails with Python Yandex
Python Yandex email sender
SMTP server email Python Yandex

Sending Email with Python Code

To facilitate email transmission in Python, we’ll be leveraging the capabilities of the smtplib and email.mime.multipart libraries. These libraries provide the necessary tools to interact with an SMTP server and construct multipart email messages, respectively. Below, you’ll find a straightforward script that exemplifies how to send an email using these resources:

Part 1: Importing Libraries
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
  • smtplib: This library is used for sending mail. It defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
  • email.mime.multipart.MIMEMultipart: This is used to create MIME messages that are multipart. Multipart messages can include both text and attachments.
  • email.mime.text.MIMEText: This class is used to create MIME objects of major type text.
Part 2: Importing Libraries
# SMTP server settings
smtp_server = ""
port = 587  # Standard port for SMTP
username = ""
password = "yourpassword"

Here, you define the SMTP server settings that will be used to send the email. This includes the SMTP server address for Yandex, the port number (587 is commonly used for SMTP with TLS encryption), and your Yandex account credentials (username and password).

Part 3: Creating the Email Message
# Creating the email message
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = ""
msg['Subject'] = "Test Email Sent With Python"
  • MIMEMultipart object is created to represent the email. The FromTo, and Subject headers are added to the message.
# The body of the email
message = "This is a test email sent using Python."
msg.attach(MIMEText(message, 'plain'))
  • Here, you define the body of the email as plain text and attach it to the message object.
Part 4: Establishing a Connection and Sending the Email
    server = smtplib.SMTP(smtp_server, port)
    server.ehlo()  # Can be crucial for identifying oneself to the server
    server.starttls()  # Initiates a secure TLS connection
    server.login(username, password)  # Authentication
    server.sendmail(msg['From'], msg['To'], msg.as_string())  # Sending the email
    print("Email sent successfully.")
except Exception as e:
    print(f"An error occurred while sending the email: {e}")
  • This section attempts to:
    • Connect to the SMTP server: A connection is established with the Yandex SMTP server using the specified port.
    • EHLO Command: It sends the EHLO command to the server, which is a greeting and can be crucial for identifying the client to the server.
    • Start TLS: It initiates a TLS connection for secure email transmission.
    • Login: Authenticates to the server using the provided username and password.
    • Send Email: The email is sent using the sendmail method of the server object. The msg.as_string() method converts the MIMEMultipart object into a string to send.
    • Quit: Finally, it terminates the connection to the server.
    • If an error occurs during this process, it is caught and printed to the console.

In this script, we initiate a connection to the Yandex SMTP server, assemble an email using the specified parameters, and execute the sending process. It’s a clear example of how Python’s email-sending capabilities can be harnessed effectively, especially when dealing with external SMTP servers like Yandex’s.

Security and Tips

  1. Password Security: Avoid using plain text passwords in your code. Consider using environment variables or password managers instead.
  2. SMTP Settings: Check your email provider’s SMTP settings and permissions. Some cases might require additional settings or permissions.
  3. Error Handling: Properly handle any errors that may occur during SMTP connection and email sending.


The ability to send emails through Yandex using Python, as demonstrated in the preceding sections, showcases the flexibility and power of Python for handling common but crucial tasks such as email automation. By following the outlined steps and utilizing the provided script, one can integrate email functionality into a variety of applications, ranging from simple notifications to more complex automated systems.

This process, leveraging the simplicity of Python and the reliability of Yandex’s SMTP service, is a testament to how effortlessly such integrations can be achieved. Whether you’re a beginner in Python or an experienced developer, the utility of sending emails programmatically is undeniable in various aspects of software development and digital communication.

We hope that this article serves not only as a guide to send emails using Python but also inspires you to explore the vast possibilities that Python’s libraries and email services like Yandex offer. As always, the key to success in programming is experimentation and continuous learning.

Leave a Comment

Join our Mailing list!

Get all latest news, exclusive deals and academy updates.