Generating zipped or gzipped attachment with email package?

skip at skip at
Thu May 21 09:34:09 EDT 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:

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(, _subtype=subtype)

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?


Skip Montanaro - skip at -
    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