Python Send mail to multiple accounts using smtplib

Gurusabarish
Sep 5, 2021

--

You need to enable lesssecureapps in your gmail account to send mails using smtplib.

Python code to send mail to multiple accounts

Python program to send mail more then one account. We can use python’s smtplib build-in library to send mails.

import smtplib
from email.mime.text import MIMEText
recipients = [
"example@example.com",
"example1@example.com",
"example2@example.com",
]
s = smtplib.SMTP("smtp.gmail.com", 587)
s.starttls()
s.login("yourmail@gmail.com", "password")
msg = MIMEText("""your body text""")
sender = "yourmail@gmail.com"
msg["Subject"] = "Mail subject"
msg["From"] = sender
msg["To"] = " .".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
s.quit()

Python program to send mail

This code will send a mail to only one account.

import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP("smtp.gmail.com", 587)
s.starttls()
s.login("yourmail@gmail.com", "password")
msg = MIMEText("""your body text""")
sender = "yourmail@gmail.com"
msg["Subject"] = "Mail subject"
msg["From"] = sender
msg["To"] = "receiver@example.com"
s.sendmail(sender, "receiver@example.com", msg.as_string())
s.quit()

--

--

Gurusabarish
Gurusabarish

Written by Gurusabarish

Hey there!, I’m Gurusabarish. I build things for the web. A passionate web app developer. I tend to make use of modern web technologies to build websites.

No responses yet