Created
November 26, 2014 07:35
-
-
Save pdxjohnny/16f96964a7fa2a1d39ca to your computer and use it in GitHub Desktop.
Send email via gmail
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
import smtplib | |
import sys | |
class gmail(object): | |
"""docstring for gmail""" | |
def __init__( self, username, password ): | |
self.username = username | |
self.password = password | |
self.login() | |
def login( self ): | |
self.session = smtplib.SMTP( 'smtp.gmail.com', 587 ) | |
self.session.ehlo() | |
self.session.starttls() | |
self.session.login( self.username, self.password ) | |
def send( self, to, body, subject = False ): | |
content = body | |
if subject: | |
headers = "\r\n".join(["from: " + self.username, | |
"subject: " + subject, | |
"to: " + to, | |
"mime-version: 1.0", | |
"content-type: text/html"]) | |
content = headers + "\r\n\r\n" + body | |
self.session.sendmail(self.username, to, content) | |
if __name__ == '__main__': | |
recipient = sys.argv[1] | |
body_of_email = ' '.join( sys.argv[2:] ) | |
GMAIL_USERNAME = 'your_email_address@gmail.com' | |
GMAIL_PASSWORD = 'password_you_change_diligently' | |
mailer = gmail( GMAIL_USERNAME, GMAIL_PASSWORD ) | |
mailer.send( recipient, body_of_email ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Class made from http://jmduke.com/posts/how-to-send-emails-through-python-and-gmail/