Remember! we have started a python series of Interesting Projects. We are continuing this series and in this python tutorial, I will show you how you can send email in python.
What fun it will be to automate the process of sending an email with the help of python programming language. Follow this tutorial and you will see how we can do that.
Send Email in Python
In this python project, you will see how we can create a python project that will send bulk emails to a list of emails.
Python code for sending Email
Please check the Instruction before running this code in your Ide. This python code will send emails to a list of email addresses.
import csv
from email.message import EmailMessage
import smtplib
def get_credentials(filepath):
with open("credentials.txt", "r") as f:
email_address = f.readline()
email_pass = f.readline()
return (email_address, email_pass)
def login(email_address, email_pass, s):
s.ehlo()
# start TLS for security
s.starttls()
s.ehlo()
# Authentication
s.login(email_address, email_pass)
print("login")
def send_mail():
s = smtplib.SMTP("smtp.gmail.com", 587)
email_address, email_pass = get_credentials("./credentials.txt")
login(email_address, email_pass, s)
# message to be sent
subject = "Welcome to Python"
body = """Python is an interpreted, high-level,
general-purpose programming language.\n
Created by Guido van Rossum and first released in 1991,
Python's design philosophy emphasizes code readability\n
with its notable use of significant whitespace"""
message = EmailMessage()
message.set_content(body)
message['Subject'] = subject
with open("emails.csv", newline="") as csvfile:
spamreader = csv.reader(csvfile, delimiter=" ", quotechar="|")
for email in spamreader:
s.send_message(email_address, email[0], message)
print("Send To " + email[0])
# terminating the session
s.quit()
print("sent")
if __name__ == "__main__":
send_mail()
Also Check Python Libraries for Sending Emails
Instructions
To run this program you will need to follow the following instructions. Once you follow these instructions then you can run the exact code and it will work.
Step No 1: Install required python Libraries
Install CSV module in python
pip install csv
Install email module in python
pip install email
Install smtplib module in python
pip install smtplib
Check the Complete guide to use smtplib module in Python
Step No 2: Configurations
emails.csv should contain the email addresses to send the message to.
credentials.txt should contain your SMTP server login credentials, with your user name and your password on separate lines, with no additional whitespace or other decorations.
Related Python Projects:
- Extract all links from a website in python
- Add Watermark to an Image in Python
- Random Password Generator
- Convert a json file to a csv file in python
- Write hello world in Python
- Create Analog Clock with Python Turtle Module
- Create an Alarm Clock With Python - Python Project
- Create an Amazing Art With Python Turtle - Pickachu Character
- How to Create Decimal to Binary Converter with tkinter Python
- Python Turtle Project - Create Doraemon Character
- Python Will Recommend you A Movie Name
- Search a String Inside a File in Python
Summary and Conclusion
In this article, you have seen how we can create a python program that can send emails to a list of emails. If you have any questions please leave them in the comment section.