Created
July 11, 2009 15:36
-
-
Save aseeon/145285 to your computer and use it in GitHub Desktop.
Sending Email from Python scripts using Gmail account
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
#!/usr/bin/python | |
# Thanks to Hal Otis | |
# http://www.halotis.com/2009/07/11/sending-email-from-python-using-gmail/ | |
# for the script! | |
import smtplib | |
from email.MIMEText import MIMEText | |
GMAIL_LOGIN = '[email protected]' | |
GMAIL_PASSWORD = 'password' | |
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=GMAIL_LOGIN): | |
msg = MIMEText(message) | |
msg['Subject'] = subject | |
msg['From'] = from_addr | |
msg['To'] = to_addr | |
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587 | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login(GMAIL_LOGIN,GMAIL_PASSWORD) | |
server.sendmail(from_addr, to_addr, msg.as_string()) | |
server.close() | |
if __name__=="__main__": | |
send_email('test', 'This is a test email') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment