Problems extracting attachment from email

F

foten

Hi community,

This is the task I'm struggling with:
- A user sends me an email with the subject '*****3gp*****'
including an attached .3gp-file.
- I then fetch that email, extracts the attachement and stores
it localy as a file.
- I then run a python-script that parses the stored file
and generates an excel-file with statistics of that file
at a shared driver.

I'm using getmail (http://pyropus.ca/software/getmail/)
(running on cygwin with cygwin python 2.4)
and the filter function provided to fetch my mail. getmail
starts my filter script and the mail is send using stdin.

I then read from stdin using
sys.stdin.read()
and the message is stored locally. The content of this stored file
is exactly the same as the mail sent to me.

I then parse that message using
fp = open(tmp_file,'rb')
p = email.Parser.Parser()

The problem I'm having is when I'm trying to extract the
attachement using
f=open(Filename, "wb")
f.write(msg.get_payload(decode=1))
f.close()
Not the whole message is decoded and stored!
When only trying
f.write(msg.get_payload())
I see that the last 255 bytes are missing.

How is this possible, I receive every last byte from stdin?

I then tried calling my windows python installation (2.4) instead of
the cygwin thingie, but that didn't work either.

Doing this using IDLE for windows or a cmdprompt for cygwin works
great!

Any ideas 'bout what I'm doing wrong here?

cheers

//Fredrik
 
L

Lee Harr

The problem I'm having is when I'm trying to extract the
attachement using
f=open(Filename, "wb")
f.write(msg.get_payload(decode=1))
f.close()
Not the whole message is decoded and stored!
When only trying
f.write(msg.get_payload())
I see that the last 255 bytes are missing.


What happens if you do...

m = msg.get_payload(decode=1)
f.write(m)
f.write(m)
f.close()
?

Just wondering if maybe some buffer not
being flushed properly.

Maybe could replace the 2nd f.write()
with an f.flush()
 
F

foten

Lee Harr said:
What happens if you do...

m = msg.get_payload(decode=1)
f.write(m)
f.write(m)
f.close()
?

Just wondering if maybe some buffer not
being flushed properly.

Maybe could replace the 2nd f.write()
with an f.flush()
Lee Harr said:
What happens if you do...

m = msg.get_payload(decode=1)
f.write(m)
f.write(m)
f.close()
?

Just wondering if maybe some buffer not
being flushed properly.

Maybe could replace the 2nd f.write()
with an f.flush()

Thanx for the ideas!

If I do
f.write(m)
f.write(m)
nothing is changed. After the first write I'm missing 255 bytes and the
second write just doubles the data. After this I'm missing 510 bytes.
Doing a flush doesn't help either, if I'm not misstaken, calling close()
on a file inplicitly calls flush().

get_payload() fails to deliver the correct amount of data, in this case ~82k...

This drives my crazy!
 
M

Max M

foten said:
The problem I'm having is when I'm trying to extract the
attachement using
f=open(Filename, "wb")
f.write(msg.get_payload(decode=1))
f.close()
Not the whole message is decoded and stored!
When only trying
f.write(msg.get_payload())
I see that the last 255 bytes are missing.

How is this possible, I receive every last byte from stdin?


I don't think its a Python problem. Those methods are used in a lot of
places. Your file is most likely not what it is supposed to be.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top