Extracting TIFF from emails

R

Ryan Swift

Hi, I'm new to Python, so this may be an easy solution. I'm having
trouble extracting TIFF files from incoming emails. Actually, I think
the root of my problem is that I'm having trouble reading the email
header. Does anyone have an easy solution? Thanks in advance.
 
J

John J. Lee

Hi, I'm new to Python, so this may be an easy solution. I'm having
trouble extracting TIFF files from incoming emails. Actually, I think
the root of my problem is that I'm having trouble reading the email
header. Does anyone have an easy solution? Thanks in advance.

You've found the email module, right (you probably want to use Python
2.3)?

What is the specific problem you're having?


John
 
C

Christos TZOTZIOY Georgiou

Hi, I'm new to Python, so this may be an easy solution. I'm having
trouble extracting TIFF files from incoming emails. Actually, I think
the root of my problem is that I'm having trouble reading the email
header. Does anyone have an easy solution? Thanks in advance.

Check the email package. Read about the following:

* email.message_from_file
(Assuming you have your incoming mail in a text file)
* class email.Message, its walk method

For each part in the .walk() call, check the .get_content_type() result,
and if it's an 'image/tiff', you have the image data calling
..get_payload(decode=True)

Roughly :)
 
R

Ryan Swift

I have found the email module, I am using 2.2.2.

This is my code:

if __name__ == "__main__":
server = connect(mailserver, mailuser, mailpasswd)
try:
(msgCount, msgBytes) = server.stat()
print '\nThere are', msgCount, 'mail messages, total',
msgBytes, 'bytes'
print 'Retrieving message', msgCount, '\n'
(hdr, message, octets) = server.retr(msgCount)
print 'Header:', hdr
print 'Octets:', octets
print 'Message:', message

email_file = open('email.txt', 'w')
email_file.writelines(message)
email_file.close()

for part in message.walk():
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
fp = open(os.path.join(dir, filename), 'wb')
fp.write(part.get_payload(decode=1))
fp.close()
finally:
server.quit()
print 'Closed connection

I can print message and see it with no problems. It also gets written
to 'email.txt' with no problems. It is a simple multi-part MIME email
with TIF attachment. However, I get this error when I try to walk
through it:

Traceback (most recent call last):
File "C:\Python22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 305, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=1)
File "C:\Python22\lib\site-packages\Pythonwin\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python22\lib\site-packages\Pythonwin\pywin\debugger\debugger.py",
line 591, in run
exec cmd in globals, locals
File "C:\Python22\mailtest.py", line 29, in ?
for part in message.walk():
AttributeError: 'list' object has no attribute 'walk'

Am I not properly using walk?

Thanks again.
 
J

John J. Lee

I have found the email module,

I'm not 100% sure you have. My 'the email module' I meant 'the module
named "email" from the Python standard library'.
I am using 2.2.2.

This is my code:

No it's not -- where are the imports? This isn't C, we don't like to
guess these things :)

[...]
AttributeError: 'list' object has no attribute 'walk'

Am I not properly using walk?

lists have no walk method!

Presumably you thought you had something other than a list, but you don't.


John
 
R

Ryan Swift

Apparently I am getting the email as a list, but I don't know of a way
to do it otherwise. I have read through the docs on www.python.org
for the email module, but there is not much on reading mail, rather it
focuses more on creating it. Below is my *complete* code (I am fairly
sure I am importing more than I need to), is there a way to get a
message from the server in a form other than a list? I assume this
can be done with the email module but I must be too dim to find it.
Can you provide a link to information about the standard library email
module? Once again, thanks.

import poplib, os, mailconfig, email, mimetools

mailserver = mailconfig.popservername
mailuser = mailconfig.popusername
mailpasswd = mailconfig.poppassword
dir = os.curdir

def connect(mailserver, mailuser, mailpasswd):
print '\nConnecting...'
server = poplib.POP3(mailserver)
server.user(mailuser)
server.pass_(mailpasswd)
return server


if __name__ == "__main__":
server = connect(mailserver, mailuser, mailpasswd)
try:
(msgCount, msgBytes) = server.stat()
print '\nThere are', msgCount, 'mail messages, total',
msgBytes, 'bytes'
print 'Retrieving message', msgCount, '\n'
(hdr, msg, octets) = server.retr(msgCount)
email_file = open('email.txt', 'w')
email_file.write(message)
email_file.close()
finally:
server.quit()
print 'Closed connection.'

fp = open('email.txt')
filemsg = email.message_from_file(fp)
fp.close()

for part in filemsg.walk:
print part.get_content_type()
 
J

John J. Lee

sure I am importing more than I need to), is there a way to get a
message from the server in a form other than a list? I assume this
can be done with the email module but I must be too dim to find it.

If you're getting a list, you've probably accidentally clobbered the
name that you bound to the message object.

Can you provide a link to information about the standard library email
module? Once again, thanks.

No, but http://www.python.org/ certainly can.

[...]
for part in filemsg.walk:

You're missing a function call here: you want filemsg.walk(), with the
brackets.


John
 
C

Christos TZOTZIOY Georgiou

Apparently I am getting the email as a list, but I don't know of a way
to do it otherwise.

I assume you didn't see my reply since yesterday?

I don't know the mailconfig module; does it return a list of strings
that end with '\n'? If yes, use

message = email.message_from_string(''.join(your_list_of_strings))

If not, substitute '\n' for '' in the line above.
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top