forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_sender.py
33 lines (24 loc) · 1.01 KB
/
email_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_bulk_email(gmail_user, gmail_password, recipients, subject, body):
try:
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(gmail_user, gmail_password)
for to_email in recipients:
# Set up the MIME
message = MIMEMultipart()
message['From'] = gmail_user
message['To'] = to_email
message['Subject'] = subject
# Attach the body with the msg instance
message.attach(MIMEText(body, 'plain'))
# Convert the message to a string
text = message.as_string()
# Send the email
session.sendmail(gmail_user, to_email, text)
print(f'Mail Sent to {to_email}')
session.quit()
except Exception as e:
print(f"Failed to send email. Error: {str(e)}")