Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send mail using Gmail's SMTP service
# src: https://stackoverflow.com/a/12424439/617185
def send_email(user, password, recipient, subject, body):
import smtplib
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Format the email body
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.login(FROM, password)
server_ssl.sendmail(FROM, TO, message)
server_ssl.close()
except Exception as e:
print("failed to send mail")
print(e)
else:
print 'successfully sent the mail'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment