Created
June 30, 2011 10:40
-
-
Save chriscummings/1055989 to your computer and use it in GitHub Desktop.
python sendmail
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 | |
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