Skip to content

Instantly share code, notes, and snippets.

@viperadnan-git
Last active September 5, 2022 12:16

Revisions

  1. viperadnan-git revised this gist Apr 23, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    - To delete all unread email, replace line 10 with `typ, data = box.search(None, 'UnSeen') or set value of query to `UnSeen`

    ##### Run snippet with python
    Use python3.8.0
    Use python3.8.0 or older.
    ```sh
    puthone delete_email.py
    python3 delete_email.py
    ```
  2. viperadnan-git revised this gist Apr 23, 2021. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,10 @@
    ##### Configure What to delete
    - Default it will delete all email which contains `Custom String` in subject. Change it in line 5.
    - To delete all email from a specific sender, replace line 10 with `typ, data = box.search(None, 'from', 'specific@sender.email')`
    - To delete all unread email, replace line 10 with `typ, data = box.search(None, 'UnSeen') or set value of query to `UnSeen`
    - To delete all unread email, replace line 10 with `typ, data = box.search(None, 'UnSeen') or set value of query to `UnSeen`

    ##### Run snippet with python
    Use python3.8.0
    ```sh
    puthone delete_email.py
    ```
  3. viperadnan-git created this gist Apr 23, 2021.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #### Delete Junk Emails from you Gmail Account easily with python.

    ##### Generate Mail App Password
    - Visit <https://support.google.com/accounts/answer/185833> for full guide.
    - Select `Mail` in App and `Other` in Device

    ##### Configure What to delete
    - Default it will delete all email which contains `Custom String` in subject. Change it in line 5.
    - To delete all email from a specific sender, replace line 10 with `typ, data = box.search(None, 'from', 'specific@sender.email')`
    - To delete all unread email, replace line 10 with `typ, data = box.search(None, 'UnSeen') or set value of query to `UnSeen`
    18 changes: 18 additions & 0 deletions delete_email.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import imaplib

    GmailId = "YourUsername@gmail.com"
    AppPassword = "YourAppPassword"
    query = r'(X-GM-RAW "subject:\"Custom String\"")'

    box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
    box.login(GmailId, AppPassword)
    box.select('Inbox')
    typ, data = box.search(None, query)
    count = 0
    for num in data[0].split():
    box.store(num, '+FLAGS', '\\Deleted')
    count += 1
    box.expunge()
    box.close()
    box.logout()
    print(f"Deleted {count} junk emails successfully")