Skip to content

Instantly share code, notes, and snippets.

@pdxjohnny
Created November 26, 2014 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdxjohnny/16f96964a7fa2a1d39ca to your computer and use it in GitHub Desktop.
Save pdxjohnny/16f96964a7fa2a1d39ca to your computer and use it in GitHub Desktop.
Send email via gmail
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 )
@pdxjohnny
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment