Skip to content

Commit 7cb0c97

Browse files
committed
add deleting emails tutorial
1 parent c0ed27c commit 7cb0c97

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
113113
- [How to Use Regular Expressions in Python](https://www.thepythoncode.com/article/work-with-regular-expressions-in-python). ([code](python-standard-library/regular-expressions))
114114
- [Logging in Python](https://www.thepythoncode.com/article/logging-in-python). ([code](python-standard-library/logging))
115115
- [How to Make a Chat Application in Python](https://www.thepythoncode.com/article/make-a-chat-room-application-in-python). ([code](python-standard-library/chat-application))
116+
- [How to Delete Emails in Python](https://www.thepythoncode.com/article/deleting-emails-in-python). ([code](python-standard-library/deleting-emails))
116117

117118
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
118119
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# [How to Delete Emails in Python](https://www.thepythoncode.com/article/deleting-emails-in-python)
2+
To run this:
3+
- Change `username` and `password` for real email credentials, edit the `imap.search()` line for your use case and run `delete_emails.py`:
4+
```
5+
python delete_emails.py
6+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import imaplib
2+
import email
3+
from email.header import decode_header
4+
5+
# account credentials
6+
username = "youremailaddress@provider.com"
7+
password = "yourpassword"
8+
9+
# create an IMAP4 class with SSL
10+
imap = imaplib.IMAP4_SSL("imap.gmail.com")
11+
# authenticate
12+
imap.login(username, password)
13+
# select the mailbox I want to delete in
14+
# if you want SPAM, use imap.select("SPAM") instead
15+
imap.select("INBOX")
16+
# search for specific mails by sender
17+
status, messages = imap.search(None, 'FROM "googlealerts-noreply@google.com"')
18+
# to get all mails
19+
# status, messages = imap.search(None, "ALL")
20+
# to get mails by subject
21+
# status, messages = imap.search(None, 'SUBJECT "Thanks for Subscribing to our Newsletter !"')
22+
# to get mails after a specific date
23+
# status, messages = imap.search(None, 'SINCE "01-JAN-2020"')
24+
# to get mails before a specific date
25+
# status, messages = imap.search(None, 'BEFORE "01-JAN-2020"')
26+
# convert messages to a list of email IDs
27+
messages = messages[0].split(b' ')
28+
for mail in messages:
29+
_, msg = imap.fetch(mail, "(RFC822)")
30+
# you can delete the for loop for performance if you have a long list of emails
31+
# because it is only for printing the SUBJECT of target email to delete
32+
for response in msg:
33+
if isinstance(response, tuple):
34+
msg = email.message_from_bytes(response[1])
35+
# decode the email subject
36+
subject = decode_header(msg["Subject"])[0][0]
37+
if isinstance(subject, bytes):
38+
# if it's a bytes type, decode to str
39+
subject = subject.decode()
40+
print("Deleting", subject)
41+
# mark the mail as deleted
42+
imap.store(mail, "+FLAGS", "\\Deleted")
43+
# permanently remove mails that are marked as deleted
44+
# from the selected mailbox (in this case, INBOX)
45+
imap.expunge()
46+
# close the mailbox
47+
imap.close()
48+
# logout from the account
49+
imap.logout()

0 commit comments

Comments
 (0)