Skip to content

Instantly share code, notes, and snippets.

@kmonsoor
Created October 3, 2017 13:36
Show Gist options
  • Save kmonsoor/ec45a604438895c0ca074ca1666bbeb2 to your computer and use it in GitHub Desktop.
Save kmonsoor/ec45a604438895c0ca074ca1666bbeb2 to your computer and use it in GitHub Desktop.
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