I’ve been doing some research in order to learn how to have DI send an email with an attachment. I’ve gotten some conflicting results. This thread appears to suggest that smtp_mailer can do that:
…while this one suggests that smtp_mailer cannot do it but that mailer can do that:
heres a python script I use to send multiple attachments:
takes a pipe delimited list of args with TO & file attachment dsns being lists separated by commas. (files must reside in same path).
def send_mail(send_to, subject, text, pathAttachment,lstAttachments):
server = 'outbound.randomname.com'
send_from = 'DATASERVICES_AUTO@somedomain.com'
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
assert type(send_to) == list
assert type(lstAttachments) == list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = ','.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for attachmentName in lstAttachments:
part = MIMEBase('application', "octet-stream")
IH = open(pathAttachment + '\\' + attachmentName,"rb")
inputData = IH.read()
IH.close()
part.set_payload(inputData)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % attachmentName)
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
from sys import argv
send_to,subject,text,pathAttachment,lstAttachments = argv[1].split('|')
if send_to.find(',') > -1:
send_to = send_to.split(',')
else:
send_to = [send_to]
lstAttachments = lstAttachments.split(',')
text += '\n'
send_mail(send_to,subject,text,pathAttachment,lstAttachments)