Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7cb0c97

Browse files
committed
add deleting emails tutorial
1 parentc0ed27c commit7cb0c97

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
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))
Lines changed: 6 additions & 0 deletions
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+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
importimaplib
2+
importemail
3+
fromemail.headerimportdecode_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+
formailinmessages:
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+
forresponseinmsg:
33+
ifisinstance(response,tuple):
34+
msg=email.message_from_bytes(response[1])
35+
# decode the email subject
36+
subject=decode_header(msg["Subject"])[0][0]
37+
ifisinstance(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)

[8]ページ先頭

©2009-2025 Movatter.jp