email: Content-Disposition and linebreaks with long filenames

Martin Körner me at mkone.net
Wed Apr 13 04:10:04 EDT 2005


Hi NG,

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).

If I use a standard email client and generate a mail with the same 
attachment, it creates the following header:

Content-Disposition: attachment;
  filename="This is a sample file with a very long filename 0123456789.zip"

(no line break)

Here is my code:


     msg = MIMEMultipart()
     msg['Subject']  = 'Test'
     msg['From']     = 'my at address'
     msg['To']       = 'my at address'
     msg.epilogue = ''

     # Add body
     part_body = MIMEText('blabla')
     msg.attach(part_body)

     # Add attachment
     file = 'This is a sample file with a very long filename 0123456789.zip'
     fp = open(file, 'rb')
     part_att = MIMEBase('application','zip')
     part_att.set_payload(fp.read())
     fp.close()
     part_att.add_header('Content-Disposition', 'attachment', filename=file)

     # Encode the payload using Base64
     email.Encoders.encode_base64(part_att)

     msg.attach(part_att)


Is it possible to prevent the linebreak?

Thank you
Martin



More information about the Python-list mailing list