471,351 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,351 software developers and data experts.

Extracting email attachment when is_multipart() is False

I need to write a script which should extract the attachment from a text
file, which is saved as MIME mail message. Unfortunatelly,
Message.is_multipart() returns False so msg.get_payload() returns the
complete message. What I need is the attachment only. Is it possible to do
that with standard email package without the actual string level parsing?

This is how my file/message looks like:

====== start here ========
This is a multi-part message in MIME format.

------=_NextPart_000_0026_01C3B347.DBEA9660
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

CONTENT

signature, etc

------=_NextPart_000_0026_01C3B347.DBEA9660
Content-Type: application/octet-stream;
name="filename.csv"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="filename.csv"

10012;20031118;292.67;4
101;23;19.98;2;39.96
102;24;21.89;4;87.56

------=_NextPart_000_0026_01C3B347.DBEA9660--

====== end here ========

So, I obviously need this part only:

10012;20031118;292.67;4
101;23;19.98;2;39.96
102;24;21.89;4;87.56

Python 2.3.2 on windows.

Thanks and regards,

Davor
Jul 18 '05 #1
4 2959
"Davor Cengija" <dc****************@inet.hr> writes:
I need to write a script which should extract the attachment from a text
file, which is saved as MIME mail message. Unfortunatelly,
Message.is_multipart() returns False so msg.get_payload() returns the [...] This is how my file/message looks like:

====== start here ========
This is a multi-part message in MIME format.

------=_NextPart_000_0026_01C3B347.DBEA9660
Content-Type: text/plain;

[...]

You seem to be missing the RFC 822 headers (From, To, Subject, etc.).
John
Jul 18 '05 #2
John J. Lee wrote:
"Davor Cengija" <dc****************@inet.hr> writes:
This is a multi-part message in MIME format.

------=_NextPart_000_0026_01C3B347.DBEA9660
Content-Type: text/plain;

[...]

You seem to be missing the RFC 822 headers (From, To, Subject, etc.).


Yes, that's true. The question is if it's easier to write a parser for that
kind of messages or to force the message producing application to output the
headers as well. We'll see...

Thanks
Jul 18 '05 #3
On Wed, 26 Nov 2003 08:06:15 +0100, Davor Cengija wrote:
John J. Lee wrote:
"Davor Cengija" <dc****************@inet.hr> writes:
This is a multi-part message in MIME format.

------=_NextPart_000_0026_01C3B347.DBEA9660
Content-Type: text/plain;

[...]

You seem to be missing the RFC 822 headers (From, To, Subject, etc.).


Yes, that's true. The question is if it's easier to write a parser for that
kind of messages or to force the message producing application to output the
headers as well. We'll see...


You have a third option, which I would try if you can't get the
message producer to do it correctly: slap some RFC822 headers on the
beginning, and then ignore them in the parsed message object. After
all, if the rest of the data is correctly formatted, use the existing
tested MIME parser. Prepending some "bogus" RFC822 headers would be
rather trivial to do.

-D

--
"Piracy is not a technological issue. It's a behavior issue."
--Steve Jobs

www: http://dman13.dyndns.org/~dman/ jabber: dm**@dman13.dyndns.org
Jul 18 '05 #4
Derrick 'dman' Hudson <dm**@dman13.dyndns.org> writes:
On Wed, 26 Nov 2003 08:06:15 +0100, Davor Cengija wrote:

[...]
You seem to be missing the RFC 822 headers (From, To, Subject, etc.).


Yes, that's true. The question is if it's easier to write a parser for that
kind of messages or to force the message producing application to output the
headers as well. We'll see...


You have a third option, which I would try if you can't get the
message producer to do it correctly: slap some RFC822 headers on the
beginning, and then ignore them in the parsed message object. After

[...]

Or read the docs & code for the email module, to figure out how to
persuade it to take the messages without the headers.
John
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Wald | last post: by
4 posts views Thread by Paul Schmidt | last post: by
3 posts views Thread by Joh | last post: by
1 post views Thread by Arenz, Ralph | last post: by
19 posts views Thread by 叮叮当当 | last post: by
4 posts views Thread by Sergey Dorofeev | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.