Using poplib to parse headers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Claude Neveu

    Using poplib to parse headers

    Hello,

    I am writing a Python program to check email using POP3. I've tried
    the sample code from python.org, and it works great. In other words,
    the code below successfully prints out my emails.

    import getpass, poplib, email
    M = poplib.POP3('ma il.blah.com')
    M.user('usernam e')
    M.pass_('passwo rd')
    numMessages = len(M.list()[1])
    for i in range(numMessag es):
    for j in M.retr(i+1)[1]:
    print j
    M.quit()

    However, if I understand right, the Python poplib library will also
    parse the email for me so that I can iterate through the headers,
    body, etc, of each message, and use them in my program. I think the
    method I need to use is email.message_f rom_file, but I'm having
    difficulty getting it to work. Can anyone explain me how I would
    extend the above example to do this?

    I tried to do this by using in the i loop the line:

    message = email.message_f rom_file(j)

    but I get the error: "AttributeError : 'str' object has no attribute 'readline'"

    Please forgive this very basic question. I do most of my programming
    in PHP and I'm just getting started with Python.

    Many thanks,

    J-C

  • alex23

    #2
    Re: Using poplib to parse headers

    On May 27, 8:39 am, Jean-Claude Neveu <jcn-france1...@pobo x.com>
    wrote:
    I tried to do this by using in the i loop the line:
    >
    message = email.message_f rom_file(j)
    >
    but I get the error: "AttributeError : 'str' object has no attribute 'readline'"
    Heya,

    The email module has a second function - 'message_from_s tring' - which
    might be more useful here. Currently, you're passing to
    'message_from_f ile' a string that it tries to treat as a file, hence
    it complaining about the string object not having part of the file
    object interface.

    (I've never used email or poplib, so this is just a guess...)

    - alex23

    Comment

    • Gerard Flanagan

      #3
      Re: Using poplib to parse headers

      On May 27, 12:39 am, Jean-Claude Neveu <jcn-france1...@pobo x.com>
      wrote:
      Hello,
      >
      I am writing a Python program to check email using POP3. I've tried
      the sample code from python.org, and it works great. In other words,
      the code below successfully prints out my emails.
      >
      import getpass, poplib, email
      M = poplib.POP3('ma il.blah.com')
      M.user('usernam e')
      M.pass_('passwo rd')
      numMessages = len(M.list()[1])
      for i in range(numMessag es):
      for j in M.retr(i+1)[1]:
      print j
      M.quit()
      >
      However, if I understand right, the Python poplib library will also
      parse the email for me so that I can iterate through the headers,
      body, etc, of each message, and use them in my program. I think the
      method I need to use is email.message_f rom_file, but I'm having
      difficulty getting it to work. Can anyone explain me how I would
      extend the above example to do this?
      >
      I tried to do this by using in the i loop the line:
      >
      message = email.message_f rom_file(j)
      >
      but I get the error: "AttributeError : 'str' object has no attribute 'readline'"
      >
      Please forgive this very basic question. I do most of my programming
      in PHP and I'm just getting started with Python.
      >
      Many thanks,
      >
      J-C
      Here's something I wrote when I was learning Python:



      HTH (if you can decipher it!)

      Gerard

      Comment

      • TheSaint

        #4
        Re: Using poplib to parse headers

        On 06:39, martedì 27 maggio 2008 Jean-Claude Neveu wrote:
        However, if I understand right, the Python poplib library will also
        parse the email for me so that I can iterate through the headers
        See my program at http://it.geocities.com/call_me_not_now/index.html
        You may study it.

        But for short you can use

        from email.Parser import HeaderParser
        # then you can parse the mail, body etc here below

        M = poplib.POP3('ma il.blah.com')
        M.user('usernam e')
        M.pass_('passwo rd')
        numMsgs = len(M.list()[1])
        for cnt in range(1, numMsgs +1):
        header= M.top(cnt,0)[1])

        HTH

        Comment

        • Tim Roberts

          #5
          Re: Using poplib to parse headers

          Jean-Claude Neveu <jcn-france1972@pobo x.comwrote:
          >
          >I am writing a Python program to check email using POP3. I've tried
          >the sample code from python.org, and it works great. In other words,
          >the code below successfully prints out my emails.
          >
          >import getpass, poplib, email
          >M = poplib.POP3('ma il.blah.com')
          >M.user('userna me')
          >M.pass_('passw ord')
          >numMessages = len(M.list()[1])
          >for i in range(numMessag es):
          for j in M.retr(i+1)[1]:
          print j
          >M.quit()
          >
          >However, if I understand right, the Python poplib library will also
          >parse the email for me so that I can iterate through the headers,
          >body, etc, of each message, and use them in my program. I think the
          >method I need to use is email.message_f rom_file, but I'm having
          >difficulty getting it to work. Can anyone explain me how I would
          >extend the above example to do this?
          >
          >I tried to do this by using in the i loop the line:
          >
          > message = email.message_f rom_file(j)
          >
          >but I get the error: "AttributeError : 'str' object has no attribute 'readline'"
          You've received some very confusing advice in this thread. Alex had the
          right answer, but I want to expand it a bit.

          You said "the Python poplib library will also parse the email for me". This
          is incorrect. poplib, like many of the modules of the Python standard
          library, focuses on exactly one purpose: handling the POP3 protocol. It
          will allow you to count your messages, and fetch your messages, but that's
          it, because that's all that POP3 does. It's a very simple protocol.

          Now, the standard library DOES include modules for parsing email, as you
          seem to realize. The "email" module is a very sophisticated tool for that
          purpose. However, the email module doesn't have any way to fech the mail.
          So, you need to stitch them together.

          poplib.retr gives you a string. You need to hand that string to the email
          module, and you do that using "email.message_ from_string".
          --
          Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          Working...