Rama,

I use the following function (adapted from examples I've found) to send
mail with one attachment.  It should be easy to adapt it to take
multiple attachments, but it requires a fairly recent Python.  

Hope this helps.

Reggie Dugard
Merfin, LLC


def send_attachment(to_addrs, from_addr, subject, attachment, filename,
                    subtype='octet-stream', smtp_server='default.mail.server'):
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.Utils import COMMASPACE, formatdate
    from email import Encoders

    msg = MIMEMultipart()
    msg['To'] = COMMASPACE.join(to_addrs)
    msg['From'] = from_addr
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    part = MIMEBase('application', subtype)
    part.set_payload(attachment)
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"'
                   % filename)
    msg.attach(part)
    smtplib.SMTP(smtp_server).sendmail(from_addr, to_addrs, msg.as_string())


On Thu, 2004-12-09 at 13:22 +0000, [EMAIL PROTECTED] wrote:
> I am using the below piece of code to send attachments. But the 
> attachments are being inlined. They do not arrive as attachments. I would 
> like to send multiple attachments. 
> 
> Anyhelp is appreciated.
> 
> import sys, smtplib, MimeWriter, base64, StringIO
> def SendMailExt(self):
>         REQUEST = self.REQUEST
>         FILE = REQUEST.form['fileget']
>         message = StringIO.StringIO()
>         writer = MimeWriter.MimeWriter(message)
>         writer.addheader('Subject', 'TEST REQUEST FROM ATT')
>         writer.startmultipartbody('mixed')
> 
>         # start off with a text/plain part
>         part = writer.nextpart()
>         body = part.startbody('text/plain')
>         body.write('This is a test mail from Auto Test Tool :)')
> 
>         # now add an image part
>         part = writer.nextpart()
>         part.addheader('Content-Transfer-Encoding', 'base64')
>         body = part.startbody('text/plain')
>         base64.encode(open(FILE.filename, 'rb'), body)
> 
>         # finish off
>         writer.lastpart()
> 
>         # send the mail
>         smtp = smtplib.SMTP('mercury.sophos')
>         smtp.sendmail('Auto Test [EMAIL PROTECTED]', 
> '[EMAIL PROTECTED]', message.getvalue())
>         smtp.quit()
> 
>         return 'Request Sent\n'
> _______________________________________________
> ActivePython mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython



_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython

Reply via email to