Extracting TIFF from emails

Discussion in 'Python' started by Ryan Swift, Sep 3, 2003.

  1. Ryan Swift

    Ryan Swift Guest

    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.
    Ryan Swift, Sep 3, 2003
    #1
    1. Advertising

  2. Ryan Swift

    John J. Lee Guest

    (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
    John J. Lee, Sep 3, 2003
    #2
    1. Advertising

  3. On 3 Sep 2003 07:04:17 -0700, rumours say that (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.
    Christos TZOTZIOY Georgiou, Sep 3, 2003
    #3
  4. Ryan Swift

    Ryan Swift Guest

    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.
    Ryan Swift, Sep 3, 2003
    #4
  5. Ryan Swift

    John J. Lee Guest

    (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
    John J. Lee, Sep 4, 2003
    #5
  6. Ryan Swift

    Ryan Swift Guest

    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()
    Ryan Swift, Sep 4, 2003
    #6
  7. Ryan Swift

    John J. Lee Guest

    (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
    John J. Lee, Sep 4, 2003
    #7
  8. On 4 Sep 2003 08:15:28 -0700, rumours say that (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_strings))

    If not, substitute '\n' for '' in the line above.
    --
    TZOTZIOY, I speak England very best,
    Microsoft Security Alert: the Matrix began as open source.
    Christos TZOTZIOY Georgiou, Sep 4, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken

    TIFF Files

    Ken, Jun 27, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    596
    Michael Pearson
    Jun 27, 2003
  2. T. Davis
    Replies:
    4
    Views:
    20,079
    SeanMcIlvenna
    May 19, 2009
  3. George Ter-Saakov

    PDF -> TIFF.

    George Ter-Saakov, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    466
    Steven Cheng[MSFT]
    Jan 10, 2004
  4. Ather Ali Shaikh
    Replies:
    1
    Views:
    2,821
    ASP.Confused
    Jul 23, 2004
  5. ifiaz
    Replies:
    1
    Views:
    894
    Martien Verbruggen
    Nov 19, 2003
Loading...

Share This Page