简体   繁体   English

python IMAPlib 如何将没有 UID 的电子邮件移动到子文件夹中

[英]python IMAPlib how to move emails with no UIDs into sub folders

I am using the following to move emails into subfolders, however this crashes in some cases because I am told some emails don't have UIDs.我正在使用以下方法将电子邮件移动到子文件夹中,但是在某些情况下这会崩溃,因为我被告知某些电子邮件没有 UID。 Is there an alternative to this to move emails into subfolders without using UIDs?有没有其他方法可以在不使用 UID 的情况下将电子邮件移动到子文件夹中?

def parse_uid(self, data):
    match = pattern_uid.match(data)
    return match.group('uid')

where在哪里

pattern_uid = re.compile('\d+ \(UID (?P<uid>\d+)\)')

Called this way:这样调用:

resp, data = self.m.fetch(str(self.i), "(UID)")
msg_uid = self.parse_uid(data[0].decode())

with self.i being the integer id for the given email. self.i 是给定 email 的 integer id。

The aim was to copy messages using msg_uid as follows:目的是使用 msg_uid 复制消息,如下所示:

result = self.m.uid('COPY', msg_uid, 'Inbox/VALORISATIONS/KGI')
print("Result:", result[0])
if result[0] == 'OK':
    mov, data = self.m.uid('STORE', msg_uid, '+FLAGS', '(\Deleted)')
    self.m.expunge()
    print("Mail moved to subfolder")

And this gives for some providers (Allianz, Metzler, KGI, Mitsubishi) the following error message:这为某些提供商(Allianz、Metzler、KGI、Mitsubishi)提供了以下错误消息:

Traceback (most recent call last):
  File "AutomatePDP_IMAP.py", line 3283, in <module>
    GestionBoitePDP.get_mail_deal_with_it(skipgetpasswords = None)
  File "AutomatePDP_IMAP.py", line 3264, in get_mail_deal_with_it
    msg_uid = self.parse_uid(data[0].decode())
  File "AutomatePDP_IMAP.py", line 111, in parse_uid
    return match.group('uid')
AttributeError: 'NoneType' object has no attribute 'group'

Adding the following condition in the parse_uid function:在parse_uid function中添加如下条件:

def parse_uid(self, data):
    match = pattern_uid.match(data)
    if match is not None:
        return match.group('uid')
    else:
        return "UID NA"

and then using:然后使用:

if msg_uid != "UID NA":
    result = self.m.uid('COPY', msg_uid,'Inbox/VALORISATIONS/ALAHLI')
    print("Result:", result[0])
    if result[0] == 'OK':
        mov, data = self.m.uid('STORE', msg_uid, '+FLAGS', '(\Deleted)')
        self.m.expunge()
        print("Mail moved to subfolder")

I followed Max's advice and removed self.m.expunge() from the loop and added it after the loop.我听从了 Max 的建议,从循环中删除了 self.m.expunge() 并在循环之后添加了它。

Problem solved.问题解决了。

I followed Max's advice and removed self.m.expunge() from the loop and added it after the loop.我听从了 Max 的建议,从循环中删除了 self.m.expunge() 并在循环之后添加了它。

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

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