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

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0

    trying to use e-mail module


    I am trying to supply a text e-mail file to python at the command line, and have the e-mail module print out the sender and recipient of the message. I'm having trouble finding the right functions to use in python's e-mail documentation. Any ideas?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0
    So far I've got:

    import sys
    from email.Parser import Parser

    fp = open(sys.argv[1])
    parser=Parser()
    msg = parser.parse(fp)
    fp.close()

    ----------

    This reads in the email from the file... but how do I get the 'From', 'To', and 'Subject'?
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok this should work, i havn't tested it but from what i can make out of the doc's (head still spinning) the get_all() function should return the MIME headers from a parsed email..

    get_all( name[, failobj])

    Return a list of all the values for the field named name. If there are no such named headers in the message, failobj is returned (defaults to None)
    cross your fingers..

    Code:
    import sys 
    import email 
    
    fp = open(sys.argv[1]) 
    parser=email.Parser() 
    msg = parser.parse(fp)
    subject = msg.get_all('From', None)
    subject = msg.get_all('To', None)
    subject = msg.get_all('Subject', None)
    fp.close()
    you might also find parseaddr() interest, there do seem to be allot of different ways to go about doing this

    parseaddr( address)

    Parse address - which should be the value of some address-containing field such as To: or Cc: - into its constituent realname and email address parts. Returns a tuple of that information, unless the parse fails, in which case a 2-tuple of ('', '') is returned.
    Mark.
    Last edited by netytan; October 23rd, 2003 at 05:02 PM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0

    Thanks


    Ahh thank you. My problem was that I was not using 'Subject'. That doc file is ridiculous

IMN logo majestic logo threadwatch logo seochat tools logo