June 21st, 2005, 07:55 PM
need to strip stuff from email
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 ?
June 21st, 2005, 08:16 PM
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.
June 21st, 2005, 08:34 PM
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 ""
June 21st, 2005, 08:46 PM
ok, here is the script
Mailbox = poplib.POP3('xxx.xxx.xxx')
for MessageLine in MyMessage:
and here is the return
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.
June 21st, 2005, 09:20 PM
regex, where do i find that?
June 21st, 2005, 09:25 PM
June 21st, 2005, 10:19 PM
cool enough, maybe i can work with this.
June 22nd, 2005, 02:51 PM
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 .
June 22nd, 2005, 05:49 PM
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
June 22nd, 2005, 06:39 PM
He's right, RegeX can easily be avoided here, and most people recommend not using regex when you don't have to.
Originally Posted by netytan
June 22nd, 2005, 08:13 PM
OK i found this in a tutorial, tested it and it works.
just prints the message body.
i am really not to keen on the random part.
import string, random
import StringIO, rfc822
SERVER = "pop.spam.egg"
USER = "mulder"
PASSWORD = "trustno1"
# connect to server
server = poplib.POP3(SERVER)
# 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
how would i use this line
to select a specific message from the retrieved list?
June 23rd, 2005, 11:54 AM
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 and so on. So you would end up with something like this...
after changing from the string modules split() function to use the string types split() method .
id, size = items.split()
Hope this helps,
June 23rd, 2005, 06:28 PM
THat totally worked ! thanks much !