Why Do We Need Email Automation?
In the course of work, there are times when you need to send the same email to multiple recipients by just changing the name or periodically send data collected from external sources to the required recipients.
Such repetitive tasks can consume a lot of unnecessary time and can also lead to mistakes during the simple operations.
However, by using a Python program, these repetitive email tasks
can be automated through programming.
Automating Emails with Python
To automate email sending
in Python, you need to use the smtplib
library to connect to an SMTP server and write code to send emails.
The smtplib library can be installed on your computer using the command pip install smtplib
.
Below is an example of Python code to send different emails to 3 people each consisting of a username and an email.
Note : In the CodeFriends practice environment, the recipient's email is limited to the email registered with CodeFriends to prevent abuse of email sending.
# Import the smtplib library
import smtplib
# Import libraries to compose the email
from email.mime.text import MIMEText
# Import library for composing email body and subject
from email.mime.multipart import MIMEMultipart
# Sender's email configuration
sender_email = "sender@example.com"
# Sender's email password
password = "your_password"
# Recipient list (username and email)
recipients = [
{"username": "Alice", "email": "alice@example.com"},
{"username": "Bob", "email": "bob@example.com"},
{"username": "Charlie", "email": "charlie@example.com"}
]
# SMTP server configuration
smtp_server = "smtp.example.com"
# SMTP server port number
# What is a Port?
# A number used by programs in a network to communicate
port = 587
# Function to send the email
def send_email(username, receiver_email):
# Email subject
subject = "CodeFriends Update Notice"
# Email body
body = f"Hello {username}, Here is the latest update from CodeFriends."
# Compose the email
msg = MIMEMultipart()
# Set email sender, recipient, subject
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# Add email body
msg.attach(MIMEText(body, 'plain'))
try:
# Connect to the SMTP server
server = smtplib.SMTP(smtp_server, port)
# TLS (email encryption) connection
server.starttls()
# Login to the email
server.login(sender_email, password)
# Convert email message to string
text = msg.as_string()
# Send the email
server.sendmail(sender_email, receiver_email, text)
except Exception as e:
print(f"Error occurred while sending email to {username}: {e}")
finally:
server.quit() # Disconnect from the server
# Send email to each user
for recipient in recipients:
send_email(recipient["username"], recipient["email"])
Code Explanation
1. Recipient List
The recipients
list is defined in a dictionary format with each consisting of username
and email
.
This list contains the information of the 3 users to whom the emails will be sent.
2. Email Sending Function
The send_email
function is responsible for sending an email to each user.
The function takes username
and receiver_email
as parameters and includes the username in the email subject and body.
3. Sending Emails
A for
loop is used to send emails sequentially to each user in the recipients
list.
Each user will receive an email personalized with their name.
By using this code, you can easily send customized emails to each user.
All you need to do is change the SMTP server address (smtp.example.com
), sender email, and password to actual values and run the code.
Practice
In the practice code editor, enter the email registered with CodeFriends in the receiver_email
or make sure it is correctly reflected, and run the code.
When you run the code, a test email will be sent to the email registered with CodeFriends.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.