Created
June 22, 2017 16:38
-
-
Save lunaluxie/af182e5ede01ab965d0653480c3fd4b0 to your computer and use it in GitHub Desktop.
send email using python
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
def send_email(user, pwd, recipient, subject, body): | |
import smtplib | |
gmail_user = user | |
gmail_pwd = pwd | |
FROM = user | |
TO = recipient if type(recipient) is list else [recipient] | |
SUBJECT = subject | |
TEXT = body | |
# Prepare actual message | |
message = """From: %s\nTo: %s\nSubject: %s\n\n%s | |
""" % (FROM, ", ".join(TO), SUBJECT, TEXT) | |
try: | |
server = smtplib.SMTP("smtp.gmail.com", 587) | |
server.ehlo() | |
server.starttls() | |
server.login(gmail_user, gmail_pwd) | |
server.sendmail(FROM, TO, message) | |
server.close() | |
print ('successfully sent the mail') | |
except: | |
print ("couldn't send the mail") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment