Created
April 18, 2013 16:17
-
-
Save audente/5414027 to your computer and use it in GitHub Desktop.
Python functions to send mail using GMail. Usage: from GMail import GMailServer, SendMail with GMailServer('username', 'password') as server: SendMail(server, 'fromaddr', 'toaddrs', 'subject', 'body')
This file contains hidden or 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
| # -------------------------------------------------- | |
| import smtplib | |
| class GMailServer: | |
| def __init__(self, username, password): | |
| self.server = smtplib.SMTP('smtp.gmail.com:587') | |
| self.server.starttls() | |
| self.server.login(username,password) | |
| def __enter__(self): | |
| return self.server | |
| def __exit__(self, type, value, traceback): | |
| self.server.quit() | |
| # -------------------------------------------------- | |
| from email.mime.text import MIMEText | |
| def SendMail(server, fromaddr, toaddr, subject, body): | |
| msg = MIMEText(body) | |
| msg['Subject'] = subject | |
| msg['From'] = fromaddr | |
| msg['To'] = toaddr | |
| server.sendmail(fromaddr, [toaddr], msg.as_string()) | |
| # -------------------------------------------------- | |
| ''' | |
| fromaddr = 'fromaddr@gmail.com' | |
| toaddrs = 'toaddrs@gmail.com' | |
| subject = 'subject' | |
| body = 'body text' | |
| # Credentials | |
| username = 'fromaddr@gmail.com' | |
| password = 'password' | |
| with GMailServer(username, password) as server: | |
| SendMail(server, fromaddr, toaddrs, subject, body) | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment