469,352 Members | 2,088 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Extracting TIFF from emails

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.
Jul 18 '05 #1
7 1633
ry**@ryanswift.com (Ryan Swift) writes:
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
Jul 18 '05 #2
On 3 Sep 2003 07:04:17 -0700, rumours say that ry**@ryanswift.com (Ryan
Swift) might have written:
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 :)
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #3
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.
Jul 18 '05 #4
ry**@ryanswift.com (Ryan Swift) writes:
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
Jul 18 '05 #5
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()
Jul 18 '05 #6
ry**@ryanswift.com (Ryan Swift) writes:
[...]
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
Jul 18 '05 #7
On 4 Sep 2003 08:15:28 -0700, rumours say that ry**@ryanswift.com (Ryan
Swift) might have written:
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_str ings))

If not, substitute '\n' for '' in the line above.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Curtis Justus | last post: by
reply views Thread by Will Arrowsmith | last post: by
reply views Thread by Will Arrowsmith | last post: by
6 posts views Thread by qysbc | last post: by
1 post views Thread by amit gupta | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.