Mailingliste - Einträge 2016
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [QF-Test] How to send email with an attachment.
import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders from os.path import basename from email.mime.application import MIMEApplication smtpHost = rc.lookup("smtpHost") smtpUser = rc.lookup("smtpUser") smtpPassword = rc.lookup("smtpPassword") asHtml = rc.getBool("asHtml") addstr = rc.lookup("to") addarray= addstr.split(",") addresses = [] for a in addarray: addresses.append(a.strip()) msg = MIMEMultipart() msg['From'] = rc.lookup("from") msg['Subject'] = rc.lookup("subject") msg['To'] = ",".join(addresses) if asHtml: msg = MIMEText(rc.lookup("text"),"html") else: msg = MIMEText(rc.lookup("text"),"plain") part = MIMEBase('application', "octet-stream") part.set_payload(open("text.txt", "rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="text.txt"') msg.attach(part) # Send the message via SMTP server, but don't include the # envelope header. s = smtplib.SMTP(smtpHost) if smtpUser != '': #only perform login, if user is specified s.login(smtpUser, smtpPassword) s.sendmail(rc.lookup("from"), addresses, msg.as_string()) s.quit()
|