SHARE
TWEET

Untitled

a guest Jul 17th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import email
  3. import cPickle
  4. import getpass
  5. import imaplib
  6. import datetime
  7.  
  8. IMAP_SERVER = 'imap.gmail.com'
  9. def process_inbox(mail, email_address):
  10.     return_value, emails = mail.search(None, "ALL")
  11.     if return_value != 'OK':
  12.         print "No messages."
  13.         return
  14.  
  15.     if os.path.exists('./pickled_attachments.pkl'):
  16.         pickled_emails = open('./pickled_attachments.pkl', 'r')
  17.         older_emails = cPickle.load(pickled_emails)
  18.         pickled_emails.close()
  19.         new_emails = [my_mail for my_mail in emails[0].split() if my_mail not in older_emails]
  20.         try:
  21.             for my_mail in new_emails:
  22.                 return_value, raw_data = mail.fetch(my_mail, '(RFC822)')
  23.                 if return_value != 'OK':
  24.                     print "Error getting message", my_mail
  25.                     return
  26.  
  27.                 message = email.message_from_string(raw_data[0][1])
  28.                 for part in message.walk():
  29.                     if part.get_content_type() in ['image/jpeg', 'image/png', 'image/bmp', 'application/msword', 'image/gif', 'image/x-icon', 'video/x-mpeg', 'application/mspowerpoint', 'application/pdf']:#sample attachment mimetypes, more can be added
  30.                         body = part.get_payload(decode=True)
  31.                         save_string = str("./" + email_address + "_attachments" + "/" + str(part.get_filename()))
  32.                         myfile = open(save_string, 'wb')
  33.                         myfile.write(body)
  34.                         myfile.close()
  35.                     else:
  36.                         continue
  37.                 older_emails.append(my_mail)
  38.             pickled_emails = open("./pickled_attachments.pkl", 'w')
  39.             cPickle.dump(older_emails, pickled_emails)
  40.             pickled_emails.close()
  41.         except:
  42.             pickled_emails = open("./pickled_attachments.pkl", 'w')
  43.             cPickle.dump(older_emails, pickled_emails)
  44.             pickled_emails.close()
  45.     else:
  46.         stored_emails = list()
  47.         try:
  48.             for my_mail in emails[0].split():
  49.                 return_value, raw_data = mail.fetch(my_mail, '(RFC822)')
  50.                 if return_value != 'OK':
  51.                     print "Error getting message", my_mail
  52.                     return
  53.  
  54.                 message = email.message_from_string(raw_data[0][1])
  55.                 for part in message.walk():
  56.                     if part.get_content_type() in ['image/jpeg', 'image/png', 'image/bmp', 'application/msword', 'image/gif', 'image/x-icon', 'video/x-mpeg', 'application/mspowerpoint', 'application/pdf']:#sample attachment mimetypes, more can be added
  57.                         body = part.get_payload(decode=True)
  58.                         save_string = str("./" + email_address + "_attachments" + "/" + str(part.get_filename()))
  59.                         myfile = open(save_string, 'wb')
  60.                         myfile.write(body)
  61.                         myfile.close()
  62.                     else:
  63.                         continue
  64.                 stored_emails.append(my_mail)
  65.             pickled_emails = open("./pickled_attachments.pkl", 'w')
  66.             cPickle.dump(stored_emails, pickled_emails)
  67.             pickled_emails.close()
  68.         except:
  69.             pickled_emails = open("./pickled_attachments.pkl", 'w')
  70.             cPickle.dump(stored_emails, pickled_emails)
  71.             pickled_emails.close()
  72.  
  73. def get_inbox(mail):
  74.     return_value, inbox_mail = mail.select("INBOX")
  75.     return return_value
  76.  
  77.  
  78. def main():
  79.     mail = imaplib.IMAP4_SSL(IMAP_SERVER)
  80.     return_value = 0
  81.     while return_value == 0:
  82.         email_address = raw_input('Email:')
  83.         try:
  84.             return_value, data = mail.login(email_address, getpass.getpass())
  85.         except imaplib.IMAP4.error:
  86.             print "Login failed."
  87.     if os.path.exists("./" + email_address + "_attachments"):
  88.         ret = get_inbox(mail)
  89.         if ret == "OK":
  90.             process_inbox(mail, email_address)
  91.             mail.close()
  92.     else:
  93.         os.makedirs("./" + email_address + "_attachments")
  94.         ret = get_inbox(mail)
  95.         if ret == "OK":
  96.             process_inbox(mail, email_address)
  97.             mail.close()
  98.  
  99.  
  100.     mail.logout()
  101.  
  102. main()
RAW Paste Data
Top