Python Outlook Win32-从Outlook返回“脚本”的所有消息时出错

时间:2018-11-22 15:35:15

标签: python python-3.x outlook pywin32 win32com

我正在编写一个脚本,用于使用pypiwin32在python 3.7中从发件人获取每个图像附件。

import win32com.client
import click
import os
import logging

@click.command()
@click.option('-s', '--sender', 'sender', help="Sender's mail")
@click.option('-p', '--path', 'path', help="Save path")
def main(sender: str, path: str) -> None:

"""Save all attachments from a sender"""

# Get all messages from the inbox
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # 6 is the inbox folder
messages = inbox.Items
message = messages.GetFirst()

# Get all files from the path
files_list = os.listdir(path)

message_name_list = []
message_counter = 0

while message:

    print(message)

    # Check if the message is from the sender
    if message.SenderEmailAddress == sender:

        # Check if the message name have been already used
        if message not in message_name_list:

            # Add message name to the list
            message_name_list.append(message)

            # Add one to msg counter
            message_counter += 1

            # Get the message date
            mail_date = message.SentOn.date()

            # Get all attachments from the message and get a count
            attachments = message.Attachments
            att_count = attachments.Count

            # Check if the message have attachments
            if att_count >= 1:

                # Check if there is more than one attachments
                if att_count > 1:

                    for att in range(att_count):

                        # Get attachments one by one
                        nb_att = att + 1
                        attachment = attachments.Item(nb_att)

                        # Set the filename for the attachment
                        filename = "img_" + str(mail_date) + "_" + str(message_counter) + "_" + str(nb_att) + ".jpg"

                        #check if file is already saved and save if not already saved
                        if filename not in files_list:
                            print (filename)
                            attachment.SaveAsFile(path + "\\" + filename)

                    # Go to a next message
                    message = messages.GetNext()

                else:
                    # Get the attachment 
                    attachment = attachments.Item(1)

                    # Set the filename for the attachment
                    filename = 'img_' + str(mail_date) + "_" + str(message_counter) + '.jpg'

                    #check if file is already saved and save if not already saved
                    if filename not in files_list:
                        print (filename)
                        attachment.SaveAsFile(path + "\\" + filename)

                    #Go to a next message
                    message = messages.GetNext()

if __name__ == "__main__":
    main()

通常,在终端中,我应该看到邮件的名称和每个保存的附件(在脚本中称为文件名)。但是实际上我只能在终端上看到它:

脚本

脚本

脚本

脚本

还有更多其他“脚本”

有人知道为什么不打印任何消息以及为什么打印“脚本”? 以及如何解决?

0 个答案:

没有答案