#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0

    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!

    -Ryan
    Last edited by Swifty; August 20th, 2003 at 04:56 PM.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi swifty,

    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..

    http://www.python.org/doc/current/lib/module-email.html

    Code:
    #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.
    	if name.endswith('.tif'):
                        #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))
    Hope this helps,
    Mark.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0
    I'm getting an error saying

    SyntaxError: invalid syntax
    File "C:\Python22\mailtest.py", line 48
    For data in msg.walk():
             ^
    Do I have to import something I'm not?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 .

    Take care,
    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo