August 20th, 2003, 03:52 PM
Easy way to read .tif attachments from email?
Hi, I'm new to the forum & I'm just learing Python. I apologize in advance if this question has been answered previously, I haven't seen any threads in my searches.
I'm trying to find an easy and straightforward way to extract a .tif (or any other image type, for that matter) from an email attachment and place it in a local directory.
Any help will be greatly appreciated. Thanks!
Last edited by Swifty; August 20th, 2003 at 03:56 PM.
August 20th, 2003, 06:35 PM
This script should do the basics of what you want. You may want to look at the email module in Python's docs for more info..
Hope this helps,
#get email and assign it to msg, you need to change email to your email source
msg = email.message_from_file(email)
#Step through the mine headers
For data in msg.walk():
#get a filename from the header and assign it to name
name = data.get_filename()
#if name ends with .tif write the attachment to the appropriate place.
#create the attachment data.get_payload does the actual extraction. this is then written to a file.
file = open(name, 'w').write(data.get_payload(decode = 1))
August 20th, 2003, 11:38 PM
Excellent, thanks a million! I couldn't find that Python doc for the life of me either. I'll give it a shot when I go back to work tomorrow.
August 21st, 2003, 08:36 AM
I'm getting an error saying
Do I have to import something I'm not?
August 21st, 2003, 09:36 AM
You will need to import the email module but other than that nothing. The error is being caused by 'For', if you change it to 'for' if should work fine.. sorry , musta hit shift when I was typing it hehe .