简体   繁体   English

如何使用python IMAP从gmail中删除电子邮件?

[英]How to delete email from gmail using python IMAP?

I am trying to read otp from mail and after that I want to delete that email from gmail option.我正在尝试从邮件中读取 otp,然后我想从 gmail 选项中删除该电子邮件。 I have no problem in reading email but I am not able to delete mail.我阅读电子邮件没有问题,但我无法删除邮件。 I tried some code from stackoverflow.我尝试了 stackoverflow 中的一些代码。 below is my code.下面是我的代码。

def getOtpMail(vEmail, vPaasword):
    connection = imaplib.IMAP4_SSL(IMAP_URL)  # stablish connection with IMAP server
    try:
        connection.login(vEmail, vPaasword)  # Login with userid password
    except Exception as e:
        print(e)
        return

    loopLock = True

    while loopLock:
        # fetch
        connection.select('"INBOX"', readonly=True)
        retCode, messages = connection.search(None, '(UNSEEN)')
        print(messages[0])

        latest = int(messages[0].split()[-1])

        res, msg = connection.fetch(str(latest), "(RFC822)")

        for response in msg:
            if isinstance(response, tuple):
                print('\n------------email--------------\n')
                msg = email.message_from_bytes(response[1])
                if SENDER_NAME in msg['From'] and KEYWORD in msg['Subject']:
                    loopLock = False
                # fetch required information
                    for part in msg.walk():
                        body = part.get_payload()
                        word_list = body.split()
                        index = word_list.index('verification')
                        otp = word_list[index + 3].strip('.')

                        #delete mail - below two line not working
                        connection.store(str(latest), '+FLAGS', '"[Gmail]/Trash"')
                        print(connection.expunge())

                        return otp
                else:
                    continue

I read documentation and print connection.expunge() so I got response as ('NO', [b'EXPUNGE attempt on READ-ONLY folder (Failure)']) .我阅读了文档并打印了connection.expunge()所以我得到的响应为('NO', [b'EXPUNGE attempt on READ-ONLY folder (Failure)']) I think issue I have to establish connection in WRITE mode.我认为问题我必须在 WRITE 模式下建立连接。 I am not sure about it.我不确定。

In this issue, I opened mail box in readonly mode.在本期中,我以只读模式打开邮箱。 Hence my program not able to write and store in IMAP server.因此我的程序无法在 IMAP 服务器中写入和存储。 I changed我变了

connection.select('"INBOX"', readonly=True)

to

connection.select('"INBOX"', readonly=False)

also I changed command type and flag type in store method -我还更改了 store 方法中的命令类型和标志类型 -

connection.store(str(latest), '+FLAGS', '"[Gmail]/Trash"')

to

connection.store(str(latest), '+FLAGS', '\\Deleted')

. .

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

相关问题 使用python imaplib从Gmail“删除”电子邮件? - Using python imaplib to “delete” an email from Gmail? 如何从我的Shell / Python脚本访问GMail(IMAP电子邮件)下载附加到电子邮件的zip文件并进行处理? - How to access GMail (IMAP Email) from my Shell/Python script to download a zip file attached to an email and process it? 如何使用Python pop / imap lib维护电子邮件的邮件转换(回复/转发/回复所有类似gmail的邮件)? - How to maintain mail conversion (reply / forward / reply to all like gmail) of email using Python pop/imap lib? 使用 Python imaplib 永久删除 Gmail 电子邮件 - Permanently delete Gmail email using Python imaplib IMAP,查看电子邮件的标签,Python & Gmail - IMAP, view email's labels, Python & Gmail 如何使用 IMAP 使用 Z7F5F35426B927411FC9231B56382173Z 在 Gmail 中创建草稿 - How do I create a draft in Gmail using IMAP using Python 使用 IMAP4 和 python 从 GMail 获取已加星标的邮件 - Get starred messages from GMail using IMAP4 and python 使用IMAP将gmail中的电子邮件从一个标签转移到python中的另一个标签 - Transferring emails in gmail from one label to another in python using IMAP Imaplib:如何从Gmail删除电子邮件? - Imaplib: how to delete an email from Gmail? 如何在Python中执行IMAP搜索(使用Gmail和imaplib)? - How do I perform an IMAP search in Python (using Gmail and imaplib)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM