Skip to content

Instantly share code, notes, and snippets.

@hiteshseth
Created October 1, 2023 16:08
Show Gist options
  • Save hiteshseth/89c4741a615815c3bb83f1cd6604f64e to your computer and use it in GitHub Desktop.
Save hiteshseth/89c4741a615815c3bb83f1cd6604f64e to your computer and use it in GitHub Desktop.
Type in the name of websites to check separated by comma, type in email and password to check if you ever received email with those names
import imaplib
import re
import csv
def search_emails(email_account, search_terms):
"""Searches an email account for emails containing the given search terms.
Args:
email_account: The email account to search.
search_terms: A list of search terms.
Returns:
A list of email IDs that contain the given search terms.
"""
imap_server = imaplib.IMAP4_SSL('imap.gmail.com')
imap_server.login(email_account['username'], email_account['password'])
imap_server.select('INBOX')
#print(search_terms)
search_string = ' '.join(search_terms)
#print(search_string)
# Use UID search to find emails
imap_server.uid('search', None, f'BODY "{search_string}"')
result, data = imap_server.uid('search', None, f'BODY "{search_string}"')
email_uids = data[0].split()
# Fetch the email IDs
email_ids = [uid.decode() for uid in email_uids]
#print("closing imap")
imap_server.close()
imap_server.logout()
return email_ids
def main():
try:
# Get the list of search terms from the user.
search_terms = input('Enter a list of search terms, separated by commas: ').split(',')
search_terms = [term.strip() for term in search_terms] # Strip leading/trailing spaces
# Get the email account credentials from the user.
email_account = {}
email_account['username'] = input('Enter your email address: ')
email_account['password'] = input('Enter your email password: ')
# Search the email account for emails containing the given search terms.
for i in search_terms:
print("searching for")
print(i)
email_ids = search_emails(email_account, i)
type(email_ids)
if len(email_ids)==0:
print("no email found for")
print(i)
else:
print(email_ids)
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == '__main__':
main()
@hiteshseth
Copy link
Author

If you are using gmail and your password doesn't work. Try app passwords:https://support.google.com/accounts/answer/185833

Pasting instructions below:

Create & use app passwords
Important: To create an app password, you need 2-Step Verification on your Google Account.

If you use 2-Step-Verification and get a "password incorrect" error when you sign in, you can try to use an app password.

Go to your Google Account.
Select Security.
Under "Signing in to Google," select 2-Step Verification.
At the bottom of the page, select App passwords.
Enter a name that helps you remember where you’ll use the app password.
Select Generate.
To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
Select Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment