473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1837
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bozdog | last post by:
I have recently imported several fields from a spreadsheet(excel) and wish to update a field using an update query. The particular field is imported as a text string and defines a path for a...
0
by: Curtis Justus | last post by:
Hi, I am reading in a multipage TIFF image and need to manipulate each page separately. After reading the image into an Image object, I set the page by using the SelectActiveFrame method. ...
0
by: Will Arrowsmith | last post by:
Hi All, I am trying to create a .tiff file to fax using the windows fax service FAXCOMLib. I have created an array of images (bitmaps) and converted them to 1pbb format in order to allow...
0
by: Will Arrowsmith | last post by:
Hi All, I am aiming to create a multiframe Tiff file that I can fax using the windows fax service FAXCOMLib. I have created an array of Bitmaps (pages I want in my fax doc) and succesfully...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
6
by: qysbc | last post by:
I have a web page and there is a link to open a TIFF file. The way I do it is to have the server code open a binary stream, set the content type to "image/tiff" and call Response.BinaryWrite. On...
2
by: Dave G | last post by:
I'm writing a function to look for particular emails in my Inbox and when it finds one it copies the attachment to a folder and then deletes the email. It spots the email by looking for certain...
1
by: amit gupta | last post by:
Hello, I am using QuarkXPress on Macintosh to generate EPS Files with Tiff Preview embedded in it. I have written some C code to extract Tiff Preview from EPS files generated which works pretty...
7
by: Ben | last post by:
Hi We are looking for a component that offers that offers the below for Tiff files: Image clean-up (deskew, despeckle) Printing capabilities from VB The ability to add text to image, e.g....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.