Python Bulk Email Automation using Gmail

Python-Bulk-Email-Automation
Credit: Canva

Overview: Python Bulk Email Automation

Let’s get started with Python Bulk Email Automation using Gmail. From Gmail App Password to Gmail SMTP (Simple Mail Transfer Protocol), will cover each setup. Whether you’re a business owner, marketer, or developer, automating email sending can save valuable time and effort.

What’s Next? – Python Bulk Email Automation

In this article, let’s take a quick look at behind the scenes how we can use Python to send bulk emails from Gmail. Let me guide you through the whole process of sending the bulk emails using Python.

Table of Contents

Watch Video Explanation

Question: Write a Python program to send bulk emails using Gmail.

				
					#Install required modules & libraries
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
				
			

Explanation:

To install library or module enter the following code in the terminal:

pip install smtplib email

  1. The smtplib is a Python module to connect with Gmail’s SMTP live server.
  2. The email module is a Python package to handle the emails.
  3. The MIMEMultipart is a library which adds additional features to email like: subject, body, attachment, etc.
  4. The MIMEText decides the email format: Plain or HTML (bold, italic, headings, etc.).
				
					# Gmail credentials
SENDER_EMAIL = "your_email@gmail.com"
SENDER_PASSWORD = "your_App_password"

# List of recipients
recipients = ["recipient1@example.com", "recipient2@example.com", "recipient3@example.com"]

# Email subject and body
subject = "Bulk Email Test"
body = "Hello,This is a test email sent using Python."
				
			

Steps to create App Password:

  1. Login to gmail account
  2. Go to Account manage option
  3. Go to under Security option
  4. Security > enable 2 step verification (mandatory)
  5. In search section, search App Password
  6. App Password >  enter the app name > get the App Password from Gmail.
				
					try:
     #Gmail SMTP Path and Port no.
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()  # Secure connection
    server.login(SENDER_EMAIL, SENDER_PASSWORD)
				
			

Explanation: The SMTP establishes the local host to the system to connect with Gmail’s server. The login method is used to log into the server with login details.

				
					for recipient in recipients:
        msg = MIMEMultipart()
        msg["From"] = SENDER_EMAIL
        msg["To"] = recipient
        msg["Subject"] = subject
        msg.attach(MIMEText(body, "plain"))
				
			

Explanation: The for loop is used to access each email address one by one. The attach method is used to attach all email features to the main email body.

				
					#Send email
        server.sendmail(SENDER_EMAIL, recipient, msg.as_string())
        print(f"Email sent to {recipient}")
				
			

Explanation: The sendmail method is used to start the whole process of sending the emails to the recipients.

				
					server.quit()
print("All emails sent successfully!")
				
			

Explanation: The quit() method is used to disconnect from the SMTP server after sending all emails successfully.

				
					except Exception as e:
    print(f"Error: {e}")
				
			

Explanation: The except clause is used to handle the error if found any error in the try clause. try clause let’s test the code for error while except clause raises an exception and handles the error.

Complete Code:

				
					import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Gmail credentials
SENDER_EMAIL = "your_email@gmail.com"
SENDER_PASSWORD = "your_password_or_app_password"

# List of recipients
recipients = ["recipient1@example.com", "recipient2@example.com", "recipient3@example.com"]

# Email subject and body
subject = "Bulk Email Test"
body = "Hello,This is a test email sent using Python."

# Setting up the SMTP server
try:
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()  # Secure connection
    server.login(SENDER_EMAIL, SENDER_PASSWORD)

    for recipient in recipients:
        msg = MIMEMultipart()
        msg["From"] = SENDER_EMAIL
        msg["To"] = recipient
        msg["Subject"] = subject
        msg.attach(MIMEText(body, "plain"))

        # Send email
        server.sendmail(SENDER_EMAIL, recipient, msg.as_string())
        print(f"Email sent to {recipient}")

    server.quit()
    print("All emails sent successfully!")

except Exception as e:
    print(f"Error: {e}")
				
			

Now it’s YOUR turn!

Let’s share your wonderful experience of sending bulk emails using Python with Gmail. What replies did you get from recipients funny or surprising? Don’t forget to leave a comment to YouTube Video: Python Beginner Project: How to send Bulk Email using Python.

Conclusion: Python Bulk Email Automation

Automating bulk emails with Python and Gmail streamlines communication, making it faster and more efficient. Using SMTP for sending emails allows you to automate email campaigns, newsletters, or notifications with just a few lines of code.

This project not only saves time but also eliminates manual errors, ensuring smooth and reliable email delivery. Whether you’re a business professional or a developer, Python bulk email automation is a powerful tool to enhance productivity.

Recent Articles