Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extracting TIFF from emails

Reply
Thread Tools

Extracting TIFF from emails

 
 
Ryan Swift
Guest
Posts: n/a
 
      09-03-2003
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.
 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      09-03-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      09-03-2003
On 3 Sep 2003 07:04:17 -0700, rumours say that (E-Mail Removed) (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.
 
Reply With Quote
 
Ryan Swift
Guest
Posts: n/a
 
      09-03-2003
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.
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      09-03-2003
(E-Mail Removed) (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
 
Reply With Quote
 
Ryan Swift
Guest
Posts: n/a
 
      09-04-2003
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()
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      09-04-2003
(E-Mail Removed) (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
 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      09-04-2003
On 4 Sep 2003 08:15:28 -0700, rumours say that (E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert TIFF-YC to TIFF-RGB Rubaiyat of Omar Bradley Digital Photography 11 06-27-2010 04:23 PM
BMP to TIFF or TIFF to BMP- any loss? Jenna Topping Digital Photography 5 01-23-2005 01:18 AM
Tool for converting 12-bit TIFF images to 16-bit TIFF-images? Peter Frank Digital Photography 11 12-13-2004 02:41 AM
jpeg to tiff and back to tiff ARosenblat Digital Photography 6 01-09-2004 08:53 PM
Image::Info::TIFF - multi-page tiff images page count ifiaz Perl Misc 1 11-19-2003 05:25 AM



Advertisments