简体   繁体   English

使用 Python 删除 Gmail 中的特定电子邮件

[英]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蟒蛇 v3.4.1

Or you can visit Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)或者您可以访问无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题)

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 https://github.com/ikvk/imap_tools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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