Advertisement
Guest User

Untitled

a guest
Sep 19th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import ftplib
  2. import email
  3. import getpass, imaplib
  4. import os
  5. import sys
  6.  
  7.  
  8. EMAIL_HOST = 'imap.gmail.com'
  9. EMAIL_USERNAME = 'user'
  10. EMAIL_PASSWORD = 'password'
  11. FTP_HOST = 'ftp'
  12. FTP_USERNAME = 'someuser'
  13. FTP_PASSWORD = 'secret'
  14. ssl = True
  15.  
  16. def upload(file,fileName):
  17.  
  18. file_to_send = open(file,'rb') # file to send
  19. session.storbinary('STOR %s' % fileName, file) # send the file: sender_date_time.jpg
  20. file.close() # close file and FTP
  21.  
  22.  
  23. detach_dir = '.'
  24. if 'attachments' not in os.listdir(detach_dir):
  25. os.mkdir('attachments')
  26.  
  27. try:
  28. imapSession = imaplib.IMAP4_SSL(EMAIL_HOST)
  29. typ, accountDetails = imapSession.login(EMAIL_USERNAME, EMAIL_PASSWORD)
  30. if typ != 'OK':
  31. print ('Not able to sign in!')
  32. raise
  33.  
  34. imapSession.select('[Gmail]/All Mail')
  35. typ, data = imapSession.search(None, 'ALL')
  36. if typ != 'OK':
  37. print ('Error searching Inbox.')
  38. raise
  39.  
  40. # Iterating over all emails
  41. for msgId in data[0].split(): # Split message id's and iterate
  42. typ, messageParts = imapSession.fetch(msgId, '(RFC822)') # Get seperate message
  43. if typ != 'OK':
  44. print ('Error fetching mail.')
  45. raise
  46.  
  47. emailBody = messageParts[0][1]
  48. mail = email.message_from_string(emailBody) # Parse email payload
  49. sender = mail['From'] # Save sender name
  50. for part in mail.walk():
  51. if part.get_content_maintype() == 'multipart': # Check if email consists of multiple parts. Should be the case when an email has attachments as the regular email body is always present, + attachment = multipart
  52. print (part.as_string())
  53. continue
  54. if part.get('Content-Disposition') is None:
  55. print (part.as_string())
  56. continue
  57. if part.get('Content-Type') == 'image':
  58. continue
  59. print (part.get_filename()[len(part.get_filename()-4):len(part.get_filename())])
  60. if part.get_filename()[len(part.get_filename()-4):len(part.get_filename())] == '.jpg': # check if file is jpg file
  61. continue
  62.  
  63.  
  64. fileName = currentDateTime + sender + part.get_filename() # set filename, current date, sender and original file name
  65. upload(part.get_payload(decode=True),fileName) # upload fike to ftp server
  66.  
  67. if bool(fileName): # write file to local file system
  68. filePath = os.path.join(detach_dir, 'attachments', fileName)
  69. if not os.path.isfile(filePath) :
  70. print (fileName)
  71. fp = open(filePath, 'wb')
  72. fp.write(part.get_payload(decode=True))
  73. fp.close()
  74. # Todo: set flags
  75. # imapsession.remove_flags(msgid,'UNSEEN') # Remove "unseen" flag
  76. # imapsession.add_flags(msgid,'SEEN') # Add "seen" flag
  77.  
  78. imapSession.close()
  79. imapSession.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement