【问题标题】:Python issues with specific outlook email特定 Outlook 电子邮件的 Python 问题
【发布时间】:2020-05-05 03:07:42
【问题描述】:

谁能给我关于这个问题的建议?我没有收到任何错误消息,也不知道从哪里开始。

我制作了一个程序,用于检查 Outlook 收件箱中的电子邮件是否有来自发件人列表的 pdf 附件,然后将附件保存到文件夹中。您输入开始日期并选择要搜索的天数。代码中有一行显示正在检查的所有电子邮件,即使它们与发件人/s 无关以进行调试。几个月来它一直运行良好,直到最近我收到一封来自 ebay 的电子邮件,其中在主题行中包含一个包装好的纸板箱的表情,而这封特定的电子邮件导致程序运行异常。

这是错误输出的示例 - 我输入了零以从列表中搜索“任何发件人”,并像往常一样选择了一个日期范围。第一个属性错误是由无法投递的邮件(outlook 不将其视为 43 类“邮件”)引起的。您可以看到日期范围内的第一个普通邮件项目被检查,并显示主题行等,但下一条消息是 ebay 电子邮件,它在日期范围之外。该程序跳过几天而不检查该范围内的任何其他电子邮件并结束搜索。如果我禁用输出中每封电子邮件的显示,也会发生同样的事情(所有电子邮件仍会被程序读取,但有一行仅显示相关邮件)。

该特定电子邮件似乎存在一些问题 - Ebay 不是程序检查附件列表中的发件人之一,即使我的日期范围为零天,它仍会显示一周内的电子邮件上次检查的电子邮件之前。唯一看起来独特的地方是主题行中的纸板箱表情,没有其他让我特别突出的地方。

这是输出:

Choose a sender (blank to exit): 0
Enter a starting search date in format dd-mm-yyyy (blank to exit): 05-05-2020
Enter how many days previous to search through: 3
Your search starts from 2020-05-05 00:00:00 and ends on 2020-05-02

Searching for any sender
Searching...
Attribute error (not class 43 mail) for item at mainChoice0() - .MessageClass REPORT.IPM.Note.NDR - Class 46
Looking at [...], WordPress has been updated on [...]. 2020-05-05
Looking at ???? ORDER DELIVERED: 10x Capacitive Stylus Pen Tou… 2020-04-28
Reached end of date range: final message - ???? ORDER DELIVERED: 10x Capacitive Stylus Pen Tou… 2020-04-28

如果我将有问题的电子邮件移至不同的收件箱,那么我的程序将正确检查我输入的整个日期范围内的电子邮件。

它变得更奇怪了,因为如果放在一个包含特定 ebay 电子邮件的日期范围内,那么它不会出现在输出中,而其他包含卡车或勾选框等表情的 ebay 电子邮件会被程序。

Looking at ???? ORDER UPDATE: 10x Capacitive Stylu... 2020-04-22
Looking at FAILED Schemus mail synchronization 2020-04-22
Attribute error (not class 43 mail) for item at mainChoice0() - .MessageClass REPORT.IPM.Note.NDR - Class 46
Attribute error (not class 43 mail) for item at mainChoice0() - .MessageClass REPORT.IPM.Note.NDR - Class 46
Looking at Active Directory Sync Failure 2020-04-21
Looking at ✅ ORDER CONFIRMED: 10x Capacitive Stylu... 2020-04-21

下面是收集附件的代码块:

def mainChoice0(): #COLLECT ATTACHMENTS
        #Choose sender from menu.
        senderNum = input("Choose a sender (blank to exit): ")
        if senderNum == "":
            mainChoiceDisplay0 = False
            topMenu() #Return to main menu
        else:
            senderNum = int(senderNum)

        senderChoice = listSender[senderNum]

        ####Determine search range by date
        startDate = input("Enter a starting search date in format dd-mm-yyyy (blank to exit): ")
        if startDate == "":
            mainChoiceDisplay0 = False
            topMenu()

        #strptime will convert startDate from a string to a usable date
        startDate = datetime.datetime.strptime(startDate, "%d-%m-%Y") 
        searchRange = int(input("Enter how many days previous to search through: "))
        searchDate = startDate.date() - datetime.timedelta(days = searchRange)
        print("Your search starts from", startDate, "and ends on", searchDate, "\n")

        #SEARCH ANY SENDERS
        if senderNum == 0:
            print("Searching for any sender")
            print("Searching...")

        #LOOK AT INBOX CONTENTS
            for msg in reversed(itcontents): #reversed() looks at most recent received email first based on date

                #while mainChoiceDisplay0 == True:

                    try:

                        ###!!! uncomment this to see details about every email even if they aren't relevant to the search  !!!####
                        print("Looking at", msg, msg.SentOn.date()) 

                        #msg.Sender is sender name, msg.SenderEmailAddress is the address.
                        if ( (str(msg.SenderEmailAddress) or str(msg.Subject) or str(msg.Sender) or str(msg.SentOnBehalfOfName)) in senderDict and (msg.SentOn.date() >= searchDate and msg.SentOn.date() <= startDate.date())):
                            check += 1

                            print(check, "messages from", msg.SenderEmailAddress, "on", msg.SentOn.date()) #show number of messages from relevant senders


                            for x in msg.Attachments:
                                if str(".pdf").casefold() in str(x): #casefold() checks upper or lower case combinations e.g PDF pdf Pdf
                                    x.SaveAsFile("C:\\Users\\"+emailUser+"\\Desktop\\Invoices from Outlook\\" + str(msg.SentOn.date()) + str(msg.SenderEmailAddress) + x.FileName)
                                    print("Saved attachment", x, "from", str(msg.Sender()), "on", str(msg.SentOn.date()))
                                    #break
                                    #Stop searching inbox earlier than searchDate

                        nextMail() #Look at the next most recent email, repeat until end of date range

                        #Final message to indicate end of search, with subject line and date of the final message.
                        if msg.SentOn.date() < searchDate:
                            print("Reached end of date range: final message -", msg, msg.SentOn.date(), "\n")
                            mainChoiceDisplay0 = False
                            topMenu()

                    except UnicodeEncodeError: #unsupported characters
                        print("Subject line could not be parsed", msg, msg.SentOn.date())
                        #continue

                    except OverflowError: #caused by invalid or very old date e.g 01-01-0000
                        print("Date calculation caused an overflow error, possibly because you entered a very old date e.g 01-01-0000 at mainChoice0()")
                        #break

                    except AttributeError: #The email was the wrong class for the attribute msg.Sender or you tried to use an attribute that doesn't work for that method
                        print("Attribute error (not class 43 mail) for item at mainChoice0() - .MessageClass", msg.MessageClass, '- Class', msg.Class)
                        pass

                    except ValueError:
                        print("Invalid input caused a value error in mainChoice0().")

【问题讨论】:

    标签: python-3.x date outlook


    【解决方案1】:

    每次您需要使用Items 类的Find/FindNextRestrict 方法时,不必遍历所有项目并检查单独的属性。在以下文章中阅读有关这些方法的更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2023-03-18
      相关资源
      最近更新 更多