简体   繁体   中英

Deleting Specific Emails in Gmail using Python

I keep receiving this error when I run the script that is suppose to delete emails from a specific sender ONLY:

Traceback (most recent call last):
  File "/Users/philip/Desktop/Python Projects/email_organizer/eorger.py", line 33, in <module>
    deleteEmail(email,pwd,imapserver)
  File "/Users/philip/Desktop/Python Projects/email_organizer/eorger.py", line 24, in deleteEmail
    typ,data=mail.search(None,'Robinhood@prospectusdocs.com')
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 734, in search
    typ, dat = self._simple_command(name, *criteria)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1055, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SEARCH command error: BAD [b'Could not parse command']

Here is my code that produces this error:

import imaplib

# your email address
email='test@gmail.com'
# your email password
pwd='************'

# need to input imap server to connect to gmail
imapserver='imap.gmail.com'

def deleteEmail(email,pwd,IMAP):
    mail=imaplib.IMAP4_SSL(IMAP) # connect to server
    mail.login(email,pwd) # user server to login to gmail account

    mail.select('inbox') # selects the 'inbox' to look into
    # search for specific criteria in selected inbox
    typ,data=mail.search(None,'Robinhood@prospectusdocs.com')

    for num in data[0].split():
        mail.store(num,'+FLAGS',r'(\Deleted)')

    mail.expunge()
    mail.close()
    mail.logout()

deleteEmail(email,pwd,imapserver)

Any advice?

And it works in

python v3.4.1

Or you can visit Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","mypassword")
m.select('"[Gmail]/All Mail"')

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
    result, data = m.uid('fetch', num, '(RFC822)')
    if result == 'OK':
        email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
        print('From:' + email_message['From'])

m.close()
m.logout()
from imap_tools import MailBox, A  
with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    # DELETE messages that contains 'Robinhood@mail.xx' in body from INBOX folder
    mailbox.delete(mailbox.fetch(A(body='Robinhood@mail.xx')))

https://github.com/ikvk/imap_tools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM