Email Attachment

Hi,

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:

Can somebody point me in the right direction?

Thanks,
Brian


bciampa (BOB member since 2009-02-26)

here it goes an other option too…


ganeshxp :us: (BOB member since 2008-07-17)

I would go ahead and use some sort of 3rd party tool that can send attachments and call it via a script.


DanHosler :us: (BOB member since 2013-06-19)

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)

they really should add a scripts sub forum. :yesnod:


jlynn73 :us: (BOB member since 2009-10-27)

Definitely putting this into my toolbox.


DanHosler :us: (BOB member since 2013-06-19)