How to Send Email to Multiple Recipients Using Python Smtplib

Send gmail with multiple recipients

I have tested this code and it works fine:

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

emails = []
with open('test.csv', 'r') as file: #open csv file
reader = csv.reader(file) #init the csv reader
for row in reader: #iterate through all rows in the csv file
emails.append(row[1]) #add the second element of every row (the email column) to the list "emails"
emails.pop(0) #remove the first email which is just the text "email" in the csv file that we don't want
print(emails) #remove if you wish

def sender(recipients, from_email, password, body, subj): #create a sender function. partially adapted from https://stackoverflow.com/a/51931160/8402369
msg = MIMEMultipart()

msg['Subject'] = subj
msg['From'] = from_email
msg['To'] = (', ').join(recipients)

msg.attach(MIMEText(body,'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email, password)
server.send_message(msg)
server.quit()


sender(emails, 'youremail@gmail.com', 'your_password', 'Message body', 'Subject') #send all emails

Run and edit the code online

EDIT:

Here's a version with the ability to change the email content based on the csv data:

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

csvdata = []
with open('test.csv', 'r') as file: #open csv file
reader = csv.reader(file) #init the csv reader
for row in reader: #iterate through all rows in the csv file
csvdata.append(
row #instead of adding the email to the array, add the whole row to the array
) #add the second element of every row (the email column) to the list "emails"
csvdata.pop(0)


def sender(
recipient, name_to_display, from_email, password, body, subj
): #create a sender function. partially adapted from https://stackoverflow.com/a/51931160/8402369
msg = MIMEMultipart()

msg['Subject'] = subj
msg['From'] = f'{name_to_display} <{from_email}>'
msg['To'] = recipient

msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email, password)
server.send_message(msg)
server.quit()

your_email = "email@gmail.com"
your_password = "password"
name_to_display = "The name of the sender that displays in the inbox (your name or whatever you want)"

for item in csvdata:
name = item[0] #the first column of the csv file is the name
email = item[1] #the second is the email. if you have more columns get them here like so: item[2] (change the index accordingly)
print(f'Sending email to {email}')
body = "Hi " + name + ", The rest of the content goes here"
subject = "Subject"
sender(email, name_to_display, your_email, your_password, body, subject)

Run and edit this code online

Notes:

The sender function is partially adapted from here

Is there a way of concatenating emails to multiple recipients via SMTPLib in a Pandas dataframe in Python?

Taken from this answer: How to send email to multiple recipients using python smtplib?

This is the line you have to change:

First split the email_to into a list of different emails and then join using ", "

msg['To'] = ", ".join(email_to.split(";"))

Python - using smtplib to send to multiple recipients

Have you had a look at this yet? How to send email to multiple recipients using python smtplib?

It looks like you might be dropping some of the pieces of your header too by overwriting them:

header = 'From: ' + GMAIL_USER

Instead of:

header = header + 'From: ' + GMAIL_USER

You might also want to consider using format instead, but I'm already out of my depth on Python :-)



Related Topics



Leave a reply



Submit