This script automates sending personalized emails to multiple recipients using Python's built-in smtplib
and csv
modules. It reads recipient details from a CSV file and sends customized emails to each one.
No external libraries are required as the script uses built-in Python modules:
import csv # For reading the CSV file
import smtplib # For sending emails
from email.mime.text import MIMEText # For email formatting
from email.mime.multipart import MIMEMultipart # For handling multipart emails
The script uses the following configurations:
port = 1025 # SMTP server port
SMTP_server = "localhost" # SMTP server address
sender_email = "sender@example.com" # Sender's email
receiver_email = "receiver@example.com" # Default receiver (not used in bulk emails)
- The script reads recipient details from a CSV file (
personal_data.csv
). - Expected CSV format:
name,email,grade John Doe,johndoe@example.com,A Jane Smith,janesmith@example.com,B
-
Reading Data from CSV
with open("personal_data.csv") as file: reader = csv.reader(file) next(reader) # Skip the header row
- Opens and reads
personal_data.csv
. - Skips the header row to process only recipient data.
- Opens and reads
-
Sending Emails
for name, email, grade in reader: with smtplib.SMTP(SMTP_server, port) as server: server.sendmail(sender_email, email, message.format(name=name, grade=grade)) print(f"Sending email to {name}")
- Iterates over each recipient.
- Sends a personalized email using
smtplib.SMTP.sendmail()
. - Prints a confirmation message.
-
Email Message Format
message = """ subject: Your Grade Dear {name}, Your grade is {grade}. """
- Uses a simple text format.
- Dynamically inserts recipient’s
name
andgrade
.
The script sends personalized emails to all recipients listed in the CSV file.
Correct Answer: A. The code sends personalized emails to the recipients in the CSV file.
- SMTP Server Requirement: Ensure a local or external SMTP server is running on port
1025
. - Email Formatting: Consider using
MIMEText
for better email structuring. - Security: Avoid hardcoding sender credentials. Use environment variables instead.
- Error Handling: Implement exception handling to manage connection failures or invalid email addresses.
- Prepare the CSV File with recipient names, emails, and grades.
- Run the Script:
python bulk_email_sender.py
- Monitor the Output for confirmation messages.
This script provides an efficient way to send bulk personalized emails, ideal for notifications, grading reports, or announcements. 🚀