Created
October 3, 2017 13:36
-
-
Save kmonsoor/ec45a604438895c0ca074ca1666bbeb2 to your computer and use it in GitHub Desktop.
Send mail using Gmail's SMTP service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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