Skip to content

Instantly share code, notes, and snippets.

@chriscummings
Created June 30, 2011 10:40
Show Gist options
  • Save chriscummings/1055989 to your computer and use it in GitHub Desktop.
Save chriscummings/1055989 to your computer and use it in GitHub Desktop.
python sendmail
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
class sendEmail(object):
def send(self,intext,user,password,subject,recipient):
text = "<html><head></head><body>" + intext + "</body></html>"
gmail_user = user
gmail_pwd = password
to = recipient
msg = MIMEMultipart('alternative')
part1 = MIMEText(intext, 'plain')
part2 = MIMEText(text, 'html')
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(part1)
msg.attach(part2)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
# Your code here...
myGmail = sendEmail()
email_string = 'your body'
user = 'username'
psswd = 'password'
subject = 'subject'
recip = 'to address'
myGmail.send(email_string,user,psswd,subject,recip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment