-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsend_gmail.py
More file actions
31 lines (23 loc) · 740 Bytes
/
send_gmail.py
File metadata and controls
31 lines (23 loc) · 740 Bytes
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
"""
Send email via gmail SMTP
"""
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def create_message(user, recipients, subject, body):
msg = MIMEMultipart()
msg['From'] = user
msg['To'] = ', '.join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(body))
return msg
def send_mail(user, password, recipients, subject, body):
msg = create_message(user, recipients, subject, body)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(user, password)
server.sendmail(user, recipients, msg.as_string())
server.close()
print('Sent email to %s' % (', '.join(recipients)))