email: Content-Disposition and linebreaks with long filenames

David Bolen db3l at fitlinxx.com
Tue Apr 19 18:16:36 EDT 2005


Martin Körner <me at mkone.net> writes:

> I am using email module for creating mails with attachment (and then
> sending via smtplib).
> 
> If the name of the attachment file is longer than about 60 characters
> the filename is wrapped in the Content-Disposition header:
> 
> Content-Disposition: attachment;
> 	filename="This is a sample file with a very long filename
> 	0123456789.zip"
> 
> 
> This leads to a wrong attachment filename in email clients - the space
> after "filename" is not shown or the client displays a special
> character (the linbreak or tab before 0123456789.zip).

Yes, it would appear that the default Generator used by the Message
object to create a textual version of a message explicitly uses tab
(\t) as a continuation character rather than space - probably because
it looks a little nicer when printed.  Interestingly enough, the
default Header continuation character is just a plain space which
would work fine here.

I should point out that I believe this header format could be
considered correct, although I find RFC2822 a bit ambiguous on this
point.  It talks about runs of FWS (folding white space) in an
unfolding operation as being considered a single space (section
3.2.3).  However, I suppose someone might argue if "runs" includes a
single character.  I think it should, but obviously some e-mail
clients disagree :-)

(...)
> Is it possible to prevent the linebreak?

Should be - two approaches I can think of (msg below is the email.Message):

1) Create your own Header object for the specific header line rather than
   just storing it as a string via add_header.  For that specific header you
   can then override the default maximum line length.  Something like:

    from email.Header import Header

    cd_header = Header('Content-Disposition: attachment; filename="....."',
                       maxlinelen=998)
    msg['Content-Disposition'] = cd_header

   Note that because Header defaults to a space continuation character,
   you could also leave maxlinelen alone and let it break the line, but
   since it would break with a single space it would work right in clients.

2) Use your own Generator object to generate the textual version of the
   message (which is when the wrapping is occurring), and during the
   flattening process, disable (or set a longer value for) header wrapping.
   Something like:

   Assuming "fp" is an output File-like object:

    from email.Generator import Generator

    g = Generator(fp)
    g.flatten(msg, maxheaderlen=998)   (or maxheaderlen=0 to disable wrapping)


-- David



More information about the Python-list mailing list