Skip to content

Instantly share code, notes, and snippets.

@ataliba
Created August 30, 2012 02:02
Show Gist options
  • Save ataliba/3521644 to your computer and use it in GitHub Desktop.
Save ataliba/3521644 to your computer and use it in GitHub Desktop.
Code to get e-mails in a pop3 server and process then
#!/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('yourmail@yourdomain.com')
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
print message['subject']
pop_conn.quit()
@ataliba
Copy link
Author

ataliba commented Aug 30, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment