Last active
March 20, 2020 17:28
-
-
Save Lvl4Sword/0e2816a71fb65944ed4a0ce81eff7665 to your computer and use it in GitHub Desktop.
gmail_from_and_returnpath_headers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import email | |
import collections | |
import imaplib | |
mail = imaplib.IMAP4_SSL('imap.gmail.com') | |
mail.login('[email protected]', 'PASSWORD') | |
mail.list() | |
mail.select('inbox') | |
from_dict = collections.Counter() | |
return_path_dict = collections.Counter() | |
typ, data = mail.search(None, 'ALL') | |
for num in data[0].split(): | |
typ, data = mail.fetch(num, '(RFC822)') | |
msg = email.message_from_string(data[0][1].decode('ISO-8859-1')) | |
try: | |
from_header = email.header.decode_header(msg['From'])[0][0].lower().split('<')[-1].strip('>') | |
except TypeError as e: | |
from_header = email.header.decode_header(msg['From'])[0][0].lower().decode() | |
print(from_header) | |
try: | |
return_path_header = email.header.decode_header(msg['Return-Path'])[0][0].lower().split('<')[-1].strip('>') | |
print(return_path_header) | |
from_dict.update([from_header]) | |
return_path_dict.update([return_path_header]) | |
except TypeError: | |
print('----------') | |
print('NO RETURN-PATH FOUND!') | |
print('----------') | |
print() | |
print(from_dict) | |
print(return_path_dict) | |
mail.close() | |
mail.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment