Python 3 smtplib Library Script to Send Bulk Emails From CSV File Using csv Module Full Project For Beginners

Python 3 smtplib Library Script to Send Bulk Emails From CSV File Using csv Module Full Project For Beginners


Welcome folks today in this post we will be sending bulk emails using smtplib library in python. All the full sourice code of the application is shown below.



Get Started



In order to get started you need to install the following libraries using the pip command as shown below


pip install smtplib


After installing this library make an file and copy paste the following code



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):
    # start TLS for security
    # Authentication
    s.login(email_address, email_pass)

def send_mail():
    s = smtplib.SMTP("", 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['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

if __name__ == "__main__":


See also  Python 3 Tkinter Note Taking App Using MySQL Database GUI Desktop App Full Project For Beginners


And now to run this python script you need to create two files inside the same directory


First of all we need to create crendentials.txt




In this file as you can see we are storing email address and password of your account from which you are sending bulk emails


And secondly you need to create emails.csv file inside the same directory as shown below




Here we are storing all the email addresses to which you will be sending bulk emails one email per line


And now you can run the python script by using this command as shown below




Leave a Reply