MIME attachments and SMTP

Marc Jeurissen mjeuris at lib.ua.ac.be
Tue Sep 12 03:12:59 EDT 2000


You have to use the MimeWriter module. Here is an extract of a
'Shipper'-module I wrote.


        fd = StringIO.StringIO()
        m = MimeWriter.MimeWriter(fd)
        ...
        m.addheader('Subject',subject)
        m.addheader('MIME-version', '1.0')
        m.addheader('Reply-To',reply)
        m.flushheaders()

# If it's a message with more than 1 body, you have to use multipartbody

        if multi:
            boundary = mimetools.choose_boundary()
            m1 = m.startmultipartbody ('mixed', boundary)
            
            fd.write('This is a multi-part message in MIME format.')

        for body in bodies:
            bodycounter = bodycounter + 1
            if multi:
                m.nextpart()
            ...
            mime_type = body.get_header('content-type')
            if not has_iso_chars:
                m.addheader('Content-Transfer-Encoding', '7bit')
                s = body.open()
                enc_file = ''
            else:
                m.addheader('Content-Transfer-Encoding', 'base64')
#                m.addheader('Content-Disposition', 'inline')
                m.addheader('Content-Disposition', 'attachment')
                t = body.open()
                enc_file = tempfile.mktemp()
                s = open(enc_file, 'wb')
                base64.encode(body,s)
                s.close()
                s = open(enc_file, 'rb')

rick_at_brickroad at my-deja.com wrote:
> 
> I'm trying to set up a command line utility in
> python.. and I've set up the SMTP to specs but
> when I send a message doing:
> server = smtplib.SMTP('myserver')
> server.set_debuglevel(1)
> fromaddr = 'rickr at brickroad.net'
> toaddrs = 'rickr at brickroad.net'
> msg = 'blah blah blah blah'
> server.sendmail(fromaddr, toaddrs, msg)
> server.quit()
> 
> however, it arrives it arrives with no to: and no
> subject.
> 
> I was also wondering if someone could point me to
> more information on using mime types to add
> attachments. I've gone through the docs.. but
> can't find much information on this.
> 
> thanks
> 
> Rick
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Marc Jeurissen
University of Antwerp - Library Automation
Universiteitsplein 1, 2610 Wilrijk, Belgium
Tel   : +32 3 820 21 53 
Fax   : +32 3 820 21 59
E-mail: mjeuris at lib.ua.ac.be 
WWW   : http://www.ua.ac.be/ualib.html
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



More information about the Python-list mailing list