In this tutorial, you will learn how to create a Python program to send emails. Emails are a significant part of digital communication, whether for personal correspondence, business exchanges, or system alerts.



It is essential to have a basic knowledge of SMTP to gain a deeper understanding.

What is SMTP?

SMTP is an abbreviation for Simple Mail Transfer Protocol. It is a communication protocol that facilitates the transmission of electronic mail. SMTP is an Internet standard that enables the sending and receiving of email messages across IP networks.

Sending Emails Using Python

Python is a powerful language that comes with a built-in SMTP library for sending email via Simple Mail Transfer Protocol (SMTP). The library is called smtplib.

Here is a simple Python program to send an email:

Python Program to Send Email

#Importing the required module
import smtplib

# Set Up the SMTP Server
smtp_server = 'smtp.example.com'
port = 587  # For SSL
server = smtplib.SMTP(smtp_server,port)

# Create SMTP Session
server.starttls()

# Login to the Server
server.login("[email protected]", "sender_email_password")

# Compose the Email
from_address = "[email protected]"
to_address = "[email protected]"
subject = "Python SMTP Mail Subject"
body = "Hello, this is a test email from Python."
email = f"Subject: {subject}\n\n{body}"

# Send the Email
server.sendmail(from_address, to_address, email)

# Close the SMTP Session
server.quit()

Please replace 'smtp.example.com', '[email protected]', and 'sender_email_password' with your actual SMTP server, sender email, and password, respectively.

Keeping your login credentials secure is essential to prevent unauthorized access to your email account. Avoid storing them in plain text or script, which may compromise your security. Instead, use a secure vault system or environment variables to protect sensitive information.

If you're using Gmail SMTP, enable 'less secure apps' in your Gmail settings before emailing. However, Google recommends using 'App Passwords' for better security. An 'App Password' is a 16-digit passcode that allows less secure apps or devices to access your Google Account. You can set your 'App Password' in your Google Account Settings.

Let's analyze the program and understand it step by step.

Step 1: Import Necessary Libraries

The first step in our Python program to send emails is to import the necessary library. Python's built-in library 'smtplib' provides the functionalities required for sending emails.

import smtplib

Step 2: Set Up the SMTP Server

We need to connect to an SMTP server that can relay our email to its destination. The connection is created using the SMTP class of smtplib. Here, 'smtp.example.com' represents your SMTP server domain name, and 587 is the port number.

smtp_server = 'smtp.example.com'
port = 587  # For SSL
server = smtplib.SMTP(smtp_server,port)

Step 3: Create SMTP Session

We'll create an SMTP session and start the TLS (Transport Layer Security) for security.

server.starttls()

Step 4: Login to the Server

To login to the server, we provide the login credentials – namely the sender's email ID and password.

server.login("[email protected]", "sender_email_password")

Step 5: Compose the Email

Next, we compose the subject and body of the email. The email headers, such as From, To, and Subject, are added.

from_address = "[email protected]"
to_address = "[email protected]"
subject = "Python SMTP Mail Subject"
body = "Hello, this is a test email from Python."
email = f"Subject: {subject}\n\n{body}"

Step 6: Add Email Body

The email body is the main content of the email. It can be as simple or as detailed as you like. Here we will keep it simple.

body = "Hello, this is a test email from Python."
email = f"Subject: {subject}\n\n{body}"

Step 7: Send the Email

The composed email is sent using the sendmail method, which takes the sender's email address, the receiver's email address, and the message as parameters.

server.sendmail(from_address, to_address, email)

Step 8: Close the SMTP Session

Finally, we close the connection to the server with the quit method.

server.quit()


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram