Skip to content

Instantly share code, notes, and snippets.

@andrewp-as-is
Last active October 13, 2021 19:22
Show Gist options
  • Save andrewp-as-is/47d86814292afa9abd5969ec4e297f4a to your computer and use it in GitHub Desktop.
Save andrewp-as-is/47d86814292afa9abd5969ec4e297f4a to your computer and use it in GitHub Desktop.
#python #gmail
#!/usr/bin/env python
import imaplib
import email
# Sign in using App Passwords
# https://support.google.com/accounts/answer/185833
# App passwords
# https://myaccount.google.com/security
USERNAME = ""
PASSWORD = ""
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT = 993
def gmail_checker(username, password):
try:
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(username, password)
mail.select('inbox')
type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
for i in reversed(id_list):
typ, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode('utf-8'))
print(msg['subject'])
print(msg['from'])
except Exception as e:
print(str(e))
gmail_checker(USERNAME, PASSWORD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment