#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10

    need to strip stuff from email


    hey there,
    i have written a script that gets my email messages, but i need to
    be able to strip off all of the stuff except the body of the message so i can write that to a text file.

    is there an easy way to do this ?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Can you show us your current code? And an example of what the text that needs to be stripped looks like? If you do, I will attempt (not knowing the situation, I don't know if I can help or not) to help you out.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    /home/eddie/.secret
    Posts
    273
    Rep Power
    13
    If the header part (what you don't want) ends with a strange format (say full line of "-"'s) then you can use regex to make everything before it into ""
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    ok, here is the script
    Code:
    import poplib
    
    Mailbox = poplib.POP3('xxx.xxx.xxx')
    Mailbox.user("xxxx")
    Mailbox.pass_('xxxx')
    
    MyMessage=Mailbox.retr(1)
    FullText="" 
    PastHeaders=0
    for MessageLine in MyMessage[1]:
    	if PastHeaders==0:
    		if (len(MessageLine)==0):
    			PastHeaders=1
    		else:
    			FullText+=MessageLine+"\n"
    	Mailbox.quit()
    	print FullText

    and here is the return

    Return-Path: <origin@mailserver.net>


    Return-Path: <origin@mailserver.net>
    Received: by mailadmin.mailserver.net (CommuniGate Pro PIPE 4.2.10)
    with PIPE id 74164390; Tue, 21 Jun 2005 20:31:39 -0500
    Received: from [63.97.189.242] (account origin@mailserver.net HELO [192.168.1.2])
    by mailadmin.mailserver.net (CommuniGate Pro SMTP 4.2.10)
    with ESMTP id 74164395 for receiver@mailserver.net; Tue, 21 Jun 2005 20:31:37 -0500
    Message-ID: <42B8DC3C.5080003@mailserver.net>
    Date: Tue, 21 Jun 2005 20:34:20 -0700
    From: origin <origin@mailserver.net>


    Return-Path: <origin@mailserver.net>
    Received: by mailadmin.mailserver.net (CommuniGate Pro PIPE 4.2.10)
    with PIPE id 74164390; Tue, 21 Jun 2005 20:31:39 -0500
    Received: from [63.97.189.242] (account origin@mailserver.net HELO [192.168.1.2])
    by mailadmin.mailserver.net (CommuniGate Pro SMTP 4.2.10)
    with ESMTP id 74164395 for receiver@mailserver.net; Tue, 21 Jun 2005 20:31:37 -0500
    Message-ID: <42B8DC3C.5080003@mailserver.net>
    Date: Tue, 21 Jun 2005 20:34:20 -0700
    From: origin <origin@mailserver.net>
    User-Agent: Mozilla Thunderbird 1.0 (X11/20050108)
    X-Accept-Language: en-us, en
    MIME-Version: 1.0



    Return-Path: <origin@mailserver.net>
    Received: by mailadmin.mailserver.net (CommuniGate Pro PIPE 4.2.10)
    with PIPE id 74164390; Tue, 21 Jun 2005 20:31:39 -0500
    Received: from [63.97.189.242] (account origin@mailserver.net HELO [192.168.1.2])
    by mailadmin.mailserver.net (CommuniGate Pro SMTP 4.2.10)
    with ESMTP id 74164395 for receiver@mailserver.net; Tue, 21 Jun 2005 20:31:37 -0500
    Message-ID: <42B8DC3C.5080003@mailserver.net>
    Date: Tue, 21 Jun 2005 20:34:20 -0700
    From: origin <origin@mailserver.net>
    User-Agent: Mozilla Thunderbird 1.0 (X11/20050108)
    X-Accept-Language: en-us, en
    MIME-Version: 1.0
    To: receiver@mailserver.net
    Subject: test text
    Content-Type: text/plain; charset=ISO-8859-1; format=flowed
    Content-Transfer-Encoding: 7bit
    X-Spam-Checker-Version: SpamAssassin 3.0.3-mailserver_w9f4wgtp (2005-04-27) on
    communigate
    X-Spam-Level:
    X-Spam-Status: No, score=-2.6 required=6.0 tests=AWL,BAYES_00 autolearn=ham
    version=3.0.3-mailserver_w9f4wgtp
    X-TFF-CGPSA-Version: 1.4
    X-TFF-CGPSA-Filter: Scanned
    looks like all it returned was headers, subject, and everything else but the body part was stripped out - it goes on and on like this, but something i guess is backward in my script because everything else is included. just the body is left out.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    regex, where do i find that?
    thanks
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    http://www.informatik.hu-berlin.de/Themen/manuals/python/python-texinfo/regex.html
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    cool enough, maybe i can work with this.
    thanks
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    In many cases you can do without regular expressions entirely by using Pythons string methods i.e. split() and strip(). Once you have you're program working I would consider how you could do it better without the regex .

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    ok this may really be what i want to do here.
    i am going to research it and try a few things
    geeze this shouldn't be that hard !
    thanks for the tip
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    /home/eddie/.secret
    Posts
    273
    Rep Power
    13
    Originally Posted by netytan
    In many cases you can do without regular expressions entirely by using Pythons string methods i.e. split() and strip(). Once you have you're program working I would consider how you could do it better without the regex .

    Take care,

    Mark.
    He's right, RegeX can easily be avoided here, and most people recommend not using regex when you don't have to.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    OK i found this in a tutorial, tested it and it works.
    just prints the message body.



    Code:
    import poplib
    import string, random
    import StringIO, rfc822
    
    SERVER = "pop.spam.egg"
    
    USER  = "mulder"
    PASSWORD = "trustno1"
    
    # connect to server
    server = poplib.POP3(SERVER)
    
    # login
    server.user(USER)
    server.pass_(PASSWORD)
    
    # list items on server
    resp, items, octets = server.list()
    
    # download a random message
    id, size = string.split(random.choice(items))
    resp, text, octets = server.retr(id)
    
    text = string.join(text, "\n")
    file = StringIO.StringIO(text)
    
    message = rfc822.Message(file)
    
    for k, v in message.items():
        print k, "=", v
    
    print message.fp.read()
    i am really not to keen on the random part.
    how would i use this line
    id, size = string.split(random.choice(items))
    to select a specific message from the retrieved list?
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You should be able reference items by it's index, so say you wanted to get the 1st message from the server you would split items[0] and so on. So you would end up with something like this...

    Code:
    id, size = items[0].split()
    after changing from the string modules split() function to use the string types split() method .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    70
    Rep Power
    10
    THat totally worked ! thanks much !

IMN logo majestic logo threadwatch logo seochat tools logo