Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Last active March 20, 2020 17:28
Show Gist options
  • Save Lvl4Sword/0e2816a71fb65944ed4a0ce81eff7665 to your computer and use it in GitHub Desktop.
Save Lvl4Sword/0e2816a71fb65944ed4a0ce81eff7665 to your computer and use it in GitHub Desktop.
gmail_from_and_returnpath_headers.py
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