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 ?
Edit/Delete Message
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 ?
Edit/Delete Message
A short, practical update that builds on and : the approach in the thread (reading the full POP message and printing the body) is fine, but the original snippet uses the legacy rfc822/StringIO pattern from Python 2. The rfc822 module is deprecated and was removed in Python 3 — use the modern email package instead. (web.cs.dal.ca)
Recommended workflow (robust for real mailboxes): fetch the raw message bytes from the server with poplib.retr, join the returned lines into a single bytes object, parse those bytes with email (use BytesParser or email.message_from_bytes), then extract the best “body” part (prefer text/plain, fall back to text/html if needed). Inspect the result of list() to see lines like "message-number size" and select the message number you want (convert the first token to an integer before calling retr). This follows the POP3 usage pattern. (docs.python.org)
Example (Python 3 — extracts the text/plain body and handles multipart messages):
import poplib
from email.parser import BytesParser
from email import policy
M = poplib.POP3_SSL('pop.example.com') # or poplib.POP3(...)
M.user('username')
M.pass_('password')
resp, items, octets = M.list()
msg_num = 1 # pick the message number you want (POP3 is 1-based)
resp, lines, octets = M.retr(msg_num)
raw = b'\r\n'.join(lines)
msg = BytesParser(policy=policy.default).parsebytes(raw)
body_part = msg.get_body(preferencelist=('plain',))
if body_part:
text = body_part.get_content()
else:
# fallback: search parts for a usable text/plain payload
text = ''
for part in msg.walk():
if part.get_content_type() == 'text/plain' and part.get_content_disposition() != 'attachment':
text = part.get_content()
break
# 'text' now holds the decoded message body ready to write to a file Use policy=policy.default (or BytesParser(policy=policy.default)) so you get the EmailMessage API (which provides get_body() / get_content()); otherwise you may get an older legacy Message object and miss those helpers. For deeper cases (HTML-only mail, signatures, quoted replies, or messy encodings) either walk parts and decode explicitly or use a dedicated library to strip quoted text. (docs.python.org)
Jump to Post— vegaseat 1,735You could convert your total e-mail message to a list of lines and then remove the first couple of lines. I assume that's what you want.
Jump to Post— vegaseat 1,735id, size = string.split(random.choice(items))Looks like all the line does is to pick the id for the server, should be ok.
You can add a line before that to print items, to see what that looks like and go from there.
You could convert your total e-mail message to a list of lines and then remove the first couple of lines. I assume that's what you want.
yeah, the messeges come in with all kinds of tracer stuff on them, spam stuff, this and that server......
gee whiz
OK i found this in a tutorial, tested it and it works.
just prints the message body.
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?
id, size = string.split(random.choice(items)) Looks like all the line does is to pick the id for the server, should be ok.
You can add a line before that to print items, to see what that looks like and go from there.
Yeah, that is what i wound up doing.
thanks much.
What did you get? Was items a list of (id,size) tuples?
If that's the case then you have to go through all the id's in the list and retrieve the text for each with a loop. Good luck!
finally came up with this, bits and pieces from tutorials,
#!/usr/bin/python
import poplib
import string
import StringIO, rfc822
# set up server info
SERVER = "mail.xxx.net"
USER = "me"
PASSWORD = "py-junkie"
# connect to server
server = poplib.POP3(SERVER)
# login
server.user(USER)
server.pass_(PASSWORD)
# list items on server
resp, items, octets = server.list()
# download first message
id, size = items[0].split()
resp, text, octets = server.retr(id)
text = string.join(text, "\n")
file = StringIO.StringIO(text)
message = rfc822.Message(file)
print message.fp.read() works though.
thanks..
oh, yeah,
items does return the number of total messages and size.
cheers !
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.