Skip to content

Instantly share code, notes, and snippets.

@chemputer
Last active September 13, 2024 08:05
Show Gist options
  • Save chemputer/0732c1a5b2464fcea40019077abfed7b to your computer and use it in GitHub Desktop.
Save chemputer/0732c1a5b2464fcea40019077abfed7b to your computer and use it in GitHub Desktop.
MarkAllEmailsAsRead.ipynb

Mark All Emails As Read Instantly, Gmail

Very simple script executable either in Google Colab or just in Python3 to go through your Gmail (I assume it would work with other email providers that support IMAP, but have not tested it)

Two requirements:

  • IMAP
  • App Password
    • if 2FA is enabled, otherwise just your password works
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMYZKBBhIBZisrHoz2tH/gl",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/chemputer/0732c1a5b2464fcea40019077abfed7b/markallemailsasread.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"### Mark ALL Emails as Read in Gmail\n",
"- ~One version can work with Oauth 2.0 keys but I don't know how to get it to work with Colab, or the redirect URI for running locally.~\n",
"- This version is functional but uses IMAP rather than Google APIs or OAUTH and uses a personal access token (app password), though presumably it would also work with any IMAP capable email.\n",
"\n",
"\n",
"#### TODO:\n",
"- fix folder limitation in V2\n",
"- add in some logic to filter through multiple accounts"
],
"metadata": {
"id": "1IK6Wm1lYVGy"
}
},
{
"cell_type": "markdown",
"source": [
"## Version 2.0\n",
"This version works, *but* it requires a personal access token, AKA an app password, and is strongly discouraged by Google for security reasons."
],
"metadata": {
"id": "nILcsQGhY5hh"
}
},
{
"cell_type": "code",
"source": [
"import imaplib\n",
"import email\n",
"from email.header import decode_header\n",
"\n",
"# Set your Gmail credentials and app password\n",
"EMAIL = '[email protected]'\n",
"APP_PASSWORD = 'YOUR_APP_PASSWORD'\n",
"\n",
"# Connect to the Gmail IMAP server\n",
"def connect_to_imap():\n",
" try:\n",
" mail = imaplib.IMAP4_SSL('imap.gmail.com')\n",
" mail.login(EMAIL, APP_PASSWORD)\n",
" mail.select('inbox')\n",
" return mail\n",
" except Exception as e:\n",
" print(f'Failed to connect to IMAP server: {e}')\n",
" return None\n",
"\n",
"# Mark all unread messages as read\n",
"def mark_all_unread_as_read(mail):\n",
" try:\n",
" # Search for all unread emails\n",
" status, messages = mail.search(None, 'UNSEEN')\n",
" email_ids = messages[0].split()\n",
"\n",
" if not email_ids:\n",
" print('No unread messages found.')\n",
" return\n",
"\n",
" print(f'Found {len(email_ids)} unread messages.')\n",
"\n",
" # Mark all unread emails as read\n",
" for email_id in email_ids:\n",
" mail.store(email_id, '+FLAGS', '\\\\Seen')\n",
" print(f'Marked message {email_id.decode()} as read.')\n",
"\n",
" print('All unread messages have been marked as read.')\n",
"\n",
" except Exception as e:\n",
" print(f'An error occurred: {e}')\n",
"\n",
"# Connect to the IMAP server and mark messages as read, then logout\n",
"mail = connect_to_imap()\n",
"if mail:\n",
" mark_all_unread_as_read(mail)\n",
" mail.logout()\n"
],
"metadata": {
"id": "6fDgjXoRYaJZ"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment