Rev 21137 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/pythonimport smtplibfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email.MIMEText import MIMETextfrom email import Encodersimport osimport optparseCOMMASPACE = ', 'def get_attachment_part(filename):part = MIMEBase('application', 'octet-stream')part.set_payload(open(filename, 'rb').read())Encoders.encode_base64(part)part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))return partdef mail_html(from_user, from_pwd, to, subject, html_text, parts=[]):msg = MIMEMultipart()msg['From'] = from_usermsg['To'] = COMMASPACE.join(to)msg['Subject'] = subjecthtml_msg = MIMEText(html_text, 'html')msg.attach(html_msg)for part in parts:msg.attach(part)mailServer = smtplib.SMTP("smtp.gmail.com", 587)mailServer.ehlo()mailServer.starttls()mailServer.login(from_user, from_pwd)mailServer.sendmail(from_user, to, msg.as_string())# Should be mailServer.quit(), but that crashes...mailServer.close()def mail(from_user, from_pwd, to, subject, text, parts=[], cc=[], bcc=[]):msg = MIMEMultipart()msg['From'] = from_usermsg['To'] = COMMASPACE.join(to)if cc:to = to + ccmsg['Cc'] = COMMASPACE.join(cc)if bcc:to = to + bccmsg['Subject'] = subjectmsg.attach(MIMEText(text))for part in parts:msg.attach(part)mailServer = smtplib.SMTP("smtp.gmail.com", 587)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()mailServer.login(from_user, from_pwd)mailServer.sendmail(from_user, to, msg.as_string())# Should be mailServer.quit(), but that crashes...mailServer.close()def mail_send_grid(from_email, from_user, from_pwd, to, subject, text, parts=[], cc=[], bcc=[]):msg = MIMEMultipart()msg['From'] = from_emailmsg['To'] = COMMASPACE.join(to)if cc:to = to + ccmsg['Cc'] = COMMASPACE.join(cc)if bcc:to = to + bccmsg['Subject'] = subjecthtml_msg = MIMEText(text, 'html')msg.attach(html_msg)for part in parts:msg.attach(part)mailServer = smtplib.SMTP("smtp.sendgrid.net", 587)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()mailServer.login(from_user, from_pwd)mailServer.sendmail(from_user, to, msg.as_string())# Should be mailServer.quit(), but that crashes...mailServer.close()if __name__ == '__main__':mail_send_grid("sales-associates@smartdukaan.com","apikey", "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", ["kshitij.sood@saholic.com"], "hi", "text", [get_attachment_part("/home/kshitij/sa/test.xls")],["thekshitijsood@gmail.com","kshitijsood.hpu@gmail.com"],["amit.gupta@saholic.com","kshitijsood@ymail.com"])