Skip to content

Instantly share code, notes, and snippets.

@jbeluch
Created November 16, 2010 19:43
Show Gist options
  • Save jbeluch/702366 to your computer and use it in GitHub Desktop.
Save jbeluch/702366 to your computer and use it in GitHub Desktop.
Send email from a gmail address
#!/usr/bin/env python
import smtplib
class GMail(object):
hostname = 'smtp.gmail.com'
port = 587
def __init__(self, username=None, password=None):
self.server = smtplib.SMTP(self.hostname, self.port)
self.server.ehlo()
if username and password:
self.login(username, password)
def login(self, username, password):
self.username = username
self.password = password
self.server.starttls()
self.server.login(username, password)
def send_email(self, recipient=None, subject='', body='',
content_type='text/plain'):
#recipient = recipient or self.username
to = recipient or self.username
headers = ['From: %s' % self.username,
'To: %s' % to,
'MIME-Version: 1.0',
'Content-type: %s' % content_type,
'Subject: %s' % subject]
message = '\r\n'.join(headers).encode('utf-8') + '\r\n' + body
self.server.sendmail(self.username, to, message)
def logout(self):
self.server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment