473,774 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

email: Content-Disposition and linebreaks with long filenames

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@address'
msg['To'] = 'my@address'
msg.epilogue = ''

# Add body
part_body = MIMEText('blabl a')
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('appli cation','zip')
part_att.set_pa yload(fp.read() )
fp.close()
part_att.add_he ader('Content-Disposition', 'attachment', filename=file)

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

msg.attach(part _att)
Is it possible to prevent the linebreak?

Thank you
Martin
Jul 18 '05 #1
2 4661
* Martin Körner <me@mkone.net > wrote:
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) .


Yeah, this is badly applied header folding. You can circumvent it by
encoding the filename parameter according to
<http://ftp.rfc-editor.org/in-notes/rfc2184.txt>

Note that parameter continuation ala filename*1="... "; filename*2="... ." is not
really supported by MUAs, but support for filename*="..." seems to be a /bit/ better.

nd
Jul 18 '05 #2
Martin Körner <me@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=99 8) (or maxheaderlen=0 to disable wrapping)
-- David
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14237
by: lucanos | last post by:
Hey All, I'm having trouble trying to create a PHP file which will generate a multipart email message (containing both an HTML formatted part and a Plain Text formatted part). I have Googled for it, and found previous entries in Google Groups, but I just can't get it to work. My PHP File contains the following:
8
3870
by: stirrell | last post by:
Hello, One problem that I had been having is stopping email injections on contact forms. I did some research, read up on it and felt like I had created a working solution. I hadn't gotten any suspicious bouncebacks in quite some time and got many custom alerts I had set up for notifying me of injection attempts. However, just the other day, I got a bounceback from an AOL address which leads me to believe that an injection attempt was...
2
2064
by: Glenn | last post by:
hello: I am working through "Beginning PHP5 Web Development", by WROX, and I am having trouble sending HTML email messages in one application. The email goes through, but is displayed only as text/plain not text/html. Here are the headers: Date: Wed, 25 Oct 2006 14:05:03 -0400 To: XXX@gmail.com Subject: Please Confirm your postcard
4
1975
by: elampirai | last post by:
Hi, I did a feedback form in PHP. When i click the submit button, the form is submitted and the mail has been sent successfully and all the fields had been clear. But then if i click the refresh button, automatically a mail is sent with the previous data. Can anyone help me out. Here is the Code: ----------------------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>
10
19579
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email message and I cannot get it to work. I need to stay with classic asp for this. Here are some things I tried: 'CDONTS Call msg.SetLocaleIDs(65001)
0
1634
by: Nico Grubert | last post by:
Hi there, I wrote a short python script that sends an email using python's email module and I am using Python 2.3.5. The problem is, that umlauts are not displayed properly in some email clients: + On a windows machine running thunderbird 1.0.2 umlauts are displayed properly. The email header contains "Content-type: text/plain; charset=utf-8"
0
2158
by: anu b | last post by:
Now i am sending email to my friend using session variable... but my code is as below private bool SendEmail(string email) { try {
3
7243
by: anu b | last post by:
Hii I am using System.net.mail for sending email... i need to send a webpage with its html content as my email body .....for that i used mail.Isbodyhtml as true...but it is not working for me so pls help me....
27
4943
matheussousuke
by: matheussousuke | last post by:
I'm having trouble with e-mail sending, I mean, the website is suposed to send a confirmation email after sign up, but it gets like text plain instead of HTML May someone help me, please? Here's the code <?php require_once 'path_cnfg.php';
1
1797
by: runway27 | last post by:
<?php $headers = "Content-type: text/html; charset=iso-8859-1"; $to="myemailaddress"; $subject="Email Newsletter"; $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Email Newsletter</title>
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4014
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.