Inbox mail checker
Here is a simple inbox mail checker script in Python using IMAP:
import imaplib
import email
import time
# Set up your email account
username = '[email protected]'
password = 'your_password'
# Connect to the email server
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(username, password)
mail.select('inbox')
# Search for unread emails
status, response = mail.search(None, 'UNSEEN')
# Get the email IDs
email_ids = response[0].split(b' ')
# Loop through each email ID
for email_id in email_ids:
status, response = mail.fetch(email_id.decode('utf-8'), '(RFC822)')
raw_email = response[0][1].decode('utf-8')
# Parse the email
email_message = email.message_from_string(raw_email)
# Print the email subject and sender
print('Subject:', email_message['Subject'])
print('From:', email_message['From'])
# Print the email body
if email_message.is_multipart():
for part in email_message.get_payload():
if part.get_content_type() == 'text/plain':
print('Body:', part.get_payload())
else:
print('Body:', email_message.get_payload())
# Mark the email as read
status, response = mail.store(email_id.decode('utf-8'), '+FLAGS', '\\Seen')
# Close the email connection
mail.close()
mail.logout()
Please note that you need to replace '[email protected]'
and 'your_password'
with your actual Gmail email and password. Also, this script will only work if you have allowed less secure apps to access your account. If you have 2-step verification enabled, you will need to generate an App Password and use that instead of your regular password.
Also, this script will only work for Gmail. If you have a different email provider, you will need to use their IMAP server and adjust the script accordingly.
Remember to handle exceptions and errors properly in a production environment.