Automate Email Communication: Sending Emails with Python from an Email List: A Practical Guide
Introduction:
In today’s hectic environment, effective communication is crucial. Sending emails by hand can take a lot of time and be prone to mistakes when trying to reach a large number of people. With its vast libraries and features, Python offers a practical method for automating email sending procedures. In this post, we’ll look at how to send emails from an email list using Python. We’ll go over a real-world code example that shows how to send personalized emails to each recipient by reading recipient information from a file. Let’s get going!
Prerequisite:
Make an Excel or a csv document with the following information: name, email address, subject, body, and attachments (please use a comma to separate them if there are several). Excel should appear as follows:
Install below packages using pip
pip install pandas smtplib email
A Practical Guide: Step-by-Step Explanation
Import the required packages:
import smtpli
import ssl
import email
import certifi
import pandas as pd
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from pathlib import Path
import osb
Read your email list:
# to read excel sheet
df = pd.read_excel('emailList.xlsx')
# to read csv file
df = pd.read_csv('emailList.xlsx')
Create a function that sends emails.
def smtpmail(mail_reciver, subject, msg, files):
user = "YOUR_EMAIL_ADDRESS"
pwd = "YOUR_EMAIL_PASSWORD"
try:
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = user
message["To"] = mail_reciver
message["Subject"] = subject
message["Bcc"] = mail_reciver # Recommended for mass emails
# Add body to email
message.attach(MIMEText(msg, "plain"))
files = files.split(',')
for file in files: # add files to the message
file_path = file
attachment = MIMEApplication(
open(file_path, "rb").read(), _subtype="txt")
attachment.add_header('Content-Disposition',
'attachment', filename=os.path.basename(file_path))
message.attach(attachment)
# Encode file in ASCII characters to send by email
encoders.encode_base64(attachment)
# Add header as key/value pair to attachment part
text = message.as_string()
with smtplib.SMTP("smtp.gmail.com", 587) as server: # smtp.web.de
server.starttls() # Secure the connection
server.login(user, pwd)
server.sendmail(user, mail_reciver, text)
server.quit()
print(f"{mail_reciver} --->> sent")
except Exception as e:
print("\n::::: -----> Having Problme <----- ::::: ")
print(f"{mail_reciver} <<>> not sent")
print('-------------------')
print(e)
print('-------------------')
return e
Iterate through the email list:
iterate through each entry in the email list and call ``smtpmail`` function
for i in df.index:
smtpmail(mail_reciver=df['email'][i], subject=df['subject']
[i], msg=df['body'][i], files=df['attachements'][i])
Code snippet
import smtplib
import ssl
import email
import certifi
import pandas as pd
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from pathlib import Path
import os
def smtpmail(mail_reciver, subject, msg, files):
user = "YOUR_EMAIL_ADDRESS"
pwd = "YOUR_EMAIL_PASSWORD"
try:
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = user
message["To"] = mail_reciver
message["Subject"] = subject
message["Bcc"] = mail_reciver # Recommended for mass emails
# Add body to email
message.attach(MIMEText(msg, "plain"))
files = files.split(',')
for file in files: # add files to the message
file_path = file
attachment = MIMEApplication(
open(file_path, "rb").read(), _subtype="txt")
attachment.add_header('Content-Disposition',
'attachment', filename=os.path.basename(file_path))
message.attach(attachment)
# Encode file in ASCII characters to send by email
encoders.encode_base64(attachment)
# Add header as key/value pair to attachment part
text = message.as_string()
with smtplib.SMTP("smtp.gmail.com", 587) as server: # smtp.web.de
server.starttls() # Secure the connection
server.login(user, pwd)
server.sendmail(user, mail_reciver, text)
server.quit()
print(f"{mail_reciver} --->> sent")
except Exception as e:
print("\n::::: -----> Having Problme <----- ::::: ")
print(f"{mail_reciver} <<>> not sent")
print('-------------------')
print(e)
print('-------------------')
return e
if __name__ == '__main__':
# read excel file
df = pd.read_excel(
'emailList.xlsx')
for i in df.index:
smtpmail(mail_reciver=df['email'][i], subject=df['subject']
[i], msg=df['body'][i], files=df['attachements'][i])
Github repo: https://github.com/SateehTeppala/EmailList-Python