Advertisement
FocusedWolf

Gmail: Extract Blocked Attachments From Original Email

Mar 7th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import os
  2. import sys
  3. import email
  4.  
  5. """
  6. SOURCE: https://spapas.github.io/2014/10/23/retrieve-gmail-blocked-attachments/
  7.  
  8. 1. In gmail, find the email that has the blocked attachments you want to download.
  9.  
  10. 2. Click the dropdown arrow located top-right of that email.
  11.   Click "Show original"
  12.  
  13. 3. Click "Download Original".
  14.  
  15. 4. Copy the file to this folder.
  16.  
  17. 5. Open command prompt to this folder
  18.   Run this: $ python get_attachments.py original_msg.txt
  19. """
  20.  
  21. def ensure_nested_dir(nested_dir):
  22.     this_py_file = os.path.realpath(__file__)
  23.     directory = os.path.join(os.getcwd(), nested_dir)
  24.     if not os.path.exists(directory):
  25.         os.makedirs(directory)
  26.  
  27. if __name__=='__main__':
  28.     if len(sys.argv)<2:
  29.         print("Please enter a file to extract attachments from")
  30.         sys.exit(1)
  31.  
  32.     original_msg_dir = os.getcwd()
  33.     nested_dir = "Extracted Email Attachments"
  34.     ensure_nested_dir(nested_dir)
  35.     os.chdir(nested_dir)
  36.  
  37.     msg = email.message_from_file(open(os.path.join(original_msg_dir, sys.argv[1])))
  38.     for pl in msg.get_payload():
  39.         if pl.get_filename(): # if it is an attachment
  40.             open(pl.get_filename(), 'wb').write(pl.get_payload(decode=True))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement