diff --git a/mailer/tools/sendmail.py b/mailer/tools/sendmail.py index 02204c4..36eaa43 100644 --- a/mailer/tools/sendmail.py +++ b/mailer/tools/sendmail.py @@ -1,7 +1,10 @@ import smtplib from email.mime.text import MIMEText +from email.MIMEBase import MIMEBase +from email.MIMEMultipart import MIMEMultipart +from email import Encoders -def emailto(username = "internatrails@gmail.com",password="mambalam",you="internatrails@gmail.com", to=['madhuvishy@gmail.com','karthik.s.sundaram@gmail.com'], subject = "", content = "", attachmentimage = ""): +def emailto(username = "internatrails@gmail.com",password="mambalam",you="internatrails@gmail.com", to=['madhuvishy@gmail.com','karthik.s.sundaram@gmail.com'], subject = "", content = "", imagedir = "", imagefile = ""): HOST = 'smtp.gmail.com' PORT = 587 @@ -11,12 +14,24 @@ def emailto(username = "internatrails@gmail.com",password="mambalam",you="intern server.login(username,password) for emailid in to: + msg = MIMEMultipart() fp = open(content,'rb') - msg = MIMEText(fp.read()) + text = MIMEText(fp.read()) fp.close() + + #imagedir should have trailing / + fimg = open(imagedir+imagefile,'rb') + img = MIMEBase('application','octet-stream') + img.set_payload(fimg.read()) + Encoders.encode_base64(img) + img.add_header('Content-Disposition','attachment',filename=imagefile) + msg['Subject'] = subject msg['From'] = you msg['To'] = emailid - server.sendmail(you,emailid,msg.as_string()) + msg.attach(text) + msg.attach(img) + + server.sendmail(you, emailid, msg.as_string()) #attachment doesn't work yet diff --git a/mailer/tools/testemail.py b/mailer/tools/testemail.py index 281dc98..af651d1 100644 --- a/mailer/tools/testemail.py +++ b/mailer/tools/testemail.py @@ -1,3 +1,3 @@ import sendmail -sendmail.emailto(subject="Test Email", content="samplecontent.txt", to=['madhuvishy@gmail.com','karthik.s.sundaram@gmail.com','its.me.shruths@gmail.com']) +sendmail.emailto(subject="I Rock but you're No use ", content="samplecontent.txt", to=['karthik.s.sundaram@gmail.com','akivalam.007@gmail.com','madhuvishy@gmail.com'],imagedir='/home/karthik/',imagefile='sample.png')