Skip to content

Instantly share code, notes, and snippets.

@olooney
Created March 19, 2013 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olooney/5199004 to your computer and use it in GitHub Desktop.
Save olooney/5199004 to your computer and use it in GitHub Desktop.
import poplib, email, getpass
M = poplib.POP3_SSL('pop.gmail.com', 995)
username = raw_input("gmail username: ")
M.user(username)
M.pass_(getpass.getpass('%s@gmail.com password: ' % username))
status, emails, octets = M.list()
if status.startswith('+OK'):
print 'new emails:', len(emails)
full_file = open('email_full.txt', 'a')
text_file = open('email_text.txt', 'a')
summary_file = open('email_summary.txt', 'a')
for index in xrange(1, len(emails) + 1):
status, lines, octets = M.retr( index )
if status.startswith('+OK'):
raw_email = '\n'.join(lines)
message = email.message_from_string(raw_email)
# full
full_file.write(raw_email)
full_file.write('\n\n\n\n')
# text-only
text_file.write('From: ' + message.get('From', '') + '\n')
text_file.write('Date: ' + message.get('Date', '') + '\n')
text_file.write('Subject: ' + message.get('Subject', '') + '\n')
text_file.write('\n')
for part in message.walk():
if part.get_content_type() == 'text/plain':
text_file.write(part.get_payload())
summary_file.write('\n\n\n')
# header only
summary_file.write('From: ' + message.get('From', '') + '\n')
summary_file.write('Date: ' + message.get('Date', '') + '\n')
summary_file.write('Subject: ' + message.get('Subject', '') + '\n')
summary_file.write('\n\n')
full_file.close()
text_file.close()
summary_file.close()
M.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment