Created
August 30, 2012 02:02
-
-
Save ataliba/3521644 to your computer and use it in GitHub Desktop.
Code to get e-mails in a pop3 server and process then
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import poplib | |
from email import parser | |
import email | |
import os | |
import sys | |
import string | |
import re | |
pop_conn = poplib.POP3_SSL('youpopserver.yourdomain.com') | |
pop_conn.user('[email protected]') | |
pop_conn.pass_('youpass') | |
#Get messages from server: | |
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] | |
# Concat message pieces: | |
messages = ["\n".join(mssg[1]) for mssg in messages] | |
#Parse message intom an email object: | |
messages = [parser.Parser().parsestr(mssg) for mssg in messages] | |
for message in messages: | |
for part in message.walk(): | |
blockit = 0 | |
if part.get_content_maintype() == "text" and blockit == 0: | |
blockit = 1 | |
mycontent = part.get_payload() | |
mycontent = mycontent.decode("quopri_codec") | |
mycontent = re.sub('<[^>]*>', '', mycontent) | |
print mycontent | |
print message['subject'] | |
pop_conn.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is based on the tips of this thead : http://bytes.com/topic/python/answers/627485-way-extract-only-message-pop3