DEV Community

Cover image for .py: Automating Emails (Sending Personalized Emails)
Bhushan Rane
Bhushan Rane

Posted on

.py: Automating Emails (Sending Personalized Emails)

Description:

This Python script enables you to send personalized emails to a list of recipients. You can customize the sender’s email, password, subject, body, and the list of recipient emails. Please note that for security reasons, you should use an application-specific password when working with Gmail.

# Python script to send personalized emails to a list of recipients
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_personalized_email(sender_email, sender_password, recipients, subject, body):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
for recipient_email in recipients:
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = recipient_email
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
server.sendmail(sender_email, recipient_email, message.as_string())
server.quit()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)