Skip to content

Instantly share code, notes, and snippets.

@vinovator
Last active November 23, 2021 10:07
Show Gist options
  • Save vinovator/b715a8a18a3eba11049e to your computer and use it in GitHub Desktop.
Save vinovator/b715a8a18a3eba11049e to your computer and use it in GitHub Desktop.
Python script to mine IMAP Mail servers, such as yahoo, gmail etc. Edit the IMAP server name to mine a particular mail service.
# imapMailboxMiner.py
# Python 2.7.6
"""
Connect to IMAP4 server and fetch mails
http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
"""
import imaplib # Library to interact with IMPAP server
import sys
# my reusable library to prompt password using tkinter
from Commonlib.tk_PromptPassword import prompt_password
# IMAP4 server for gmail
# IMAP_server = "imap.gmail.com" # gmail IMAP server
IMAP_server = "imap.mail.yahoo.com" # Yahoo IMAP server
mail_id = "<mail_id>@yahoo.com"
if __name__ == "__main__":
"""
Entry block of the script
"""
# Instantiate IMAP4 interface
imap = imaplib.IMAP4_SSL(IMAP_server)
# Instantiate the tkinter class to prompt for password
ui = prompt_password("IMAP Mailbox Password")
pwd = ui.get_password()
# login to the server
try:
status, summary = imap.login(mail_id, pwd)
if status == "OK":
print(summary)
except imaplib.IMAP4.error:
print("Error logging into Mail")
sys.exit(0) # Successful termination
# Get list of mailboxes in server
status, mailboxes = imap.list()
if status == "OK":
print("***List of mailboxes***")
for index, mailbox in enumerate(mailboxes):
print index + 1, mailbox
else:
print("Error retreiving mailbox information - {}".format(status))
# Connect to a particular mailbox
status, data = imap.select("Inbox") # inbox name from list() used above
if status == "OK":
print("No of items in mailbox: {}".format(data))
# Process the mailbox
imap.close()
# Logout of the IMAP server
imap.logout()
@davidmponder
Copy link

Cannot install Commonlib . . . . any idea why that is?

@vinovator
Copy link
Author

Cannot install Commonlib . . . . any idea why that is?

Commonlib, is a custom library i have created to prompt for password whenever I run the program, in order to avoid storing passwords in the computer. You can explore other methods for password prompts.

@swap2ag
Copy link

swap2ag commented Jul 11, 2020

Cannot install Commonlib . . . . any idea why that is?

You can use getpass for getting password with echo off.

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