Generating zipped or gzipped attachment with email package?

skip at pobox.com skip at pobox.com
Thu May 21 15:34:09 CEST 2009


I have a script which allows me to generate MIME messages with appropriate
attachments.  It's essentially a lightly modified version of the second
example from this page of the email package docs:

    http://docs.python.org/library/email-examples.html

I want to modify my script to automatically zip or gzip files which exceed
some size threshold.  Doing the zip/gzip dance is no problem.  I'm concerned
about how to specify that properly with the email package.  For example,
consider a large CSV file.  I figure out the MIME type is text/csv.  Now
suppose I gzip the file before attaching it.  How would this code change to
specify the compression where "path" is now compressed?

    if maintype == 'text':
        fp = open(path)
        # Note: we should handle calculating the charset
        msg = MIMEText(fp.read(), _subtype=subtype)
        fp.close()

I guess I'm asking if I can have the Content-Type still be text/csv with
some other MIME header indicating the file is compressed.  If so, how do I
achieve that when attaching the compressed file to the message?

Thanks,

-- 
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/
    America's vaunted "free press" notwithstanding, story ideas that expose
    the unseemly side of actual or potential advertisers tend to fall by the
    wayside.  Not quite sure why.  -- Jim Thornton



More information about the Python-list mailing list