简体   繁体   English

想要使用 gmail 中的 imaplib python 使用 python 将所有邮件从垃圾邮件文件夹移动到收件箱

[英]Want to move all mails from spam folder to inbox with imaplib python in gmail using python

I'm trying to move all mails present in gmail's spam folder to inbox using imaplib.我正在尝试使用 imaplib 将 gmail 垃圾邮件文件夹中的所有邮件移动到收件箱。 But as a I'm facing issue like below.但作为一个我面临如下问题。

"apply_lbl_msg = obj.uid('COPY', mail_ids, "todays") AttributeError: 'str' object has no attribute 'uid' Mail IDs: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35']" "apply_lbl_msg = obj.uid('COPY', mail_ids, "todays") AttributeError: 'str' object 没有属性 'uid' 邮件 ID: ['1', '2', '3', '4', ' 5'、'6'、'7'、'8'、'9'、'10'、'11'、'12'、'13'、'14'、'15'、'16'、'17' , '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', ' 30'、'31'、'32'、'33'、'34'、'35']"

I'm able to fetch mail IDs but after that I'm not getting what to do I went through various tutorials but still facing the same issue.我能够获取邮件 ID,但在那之后我不知道该做什么,我经历了各种教程,但仍然面临同样的问题。

Please help me with this friends.Below is my code as far as I tried.If anyone knew the answer means please send me the code.So I can understand....Thanks in advance...请帮我解决这个问题。以下是我尝试过的代码。如果有人知道答案的意思,请将代码发送给我。所以我可以理解....提前谢谢...

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('my@gmail.com', 'password')
# obj.select("trials")
resp_code, mail_count = obj.select(mailbox="trials", readonly=True)

############### Retrieve Mail IDs for given Directory #############
obj, mail_ids = obj.search(None, "ALL")

print("Mail IDs : {}\n".format(mail_ids[0].decode().split()))
apply_lbl_msg = obj.uid('COPY', mail_ids, "todays")
if apply_lbl_msg[0] == 'OK':
    mov, data = obj.uid('STORE', mail_ids , '+FLAGS', '(\Deleted)')
    obj.expunge()

With above code I tried,But didn't got required result.上面的代码我试过了,但没有得到所需的结果。

While it is possible to use imaplib directly I would recommend using a more user friendly library like imap_tools :虽然可以直接使用imaplib ,但我建议使用更用户友好的库,例如imap_tools

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', initial_folder='INBOX') as mailbox:

    # MOVE all messages from current folder to INBOX/folder2
    mailbox.move(mailbox.uids(), 'INBOX/folder2')

For the specific case of Google Mail I would recommend using their Python API .对于 Google Mail 的特定情况,我建议使用他们的Python API For example I wrote a small program to filter emails using Python and the Google API, you can find the code on Github .例如,我编写了一个小程序来过滤电子邮件,使用 Python 和 Google API,您可以在Github上找到代码。

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

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