提取所有Outlook电子邮件'日期,发件人,收件人,以CSV为准

时间:2016-09-20 12:23:40

标签: python email outlook win32com archiving

简介

我想创建某种归档脚本,它会收集所有Outlook(unicode)电子邮件的日期,发件人(姓名+地址),收件人(姓名+地址) (es)),主题并将它们放在CSV文件中。

(额外的超级解决方案是,如果它可以提取包含文件夹的名称和可能的类别 - 虽然它不是必须的。 作为最后一步,我想让它变得可移植,所以其他人可以在没有Python的情况下使用它。)

(我使用的是Python 2.7和Outlook 2013)

代码

这是我到目前为止所拥有的:

import win32com.client
import sys
import unicodecsv as csv

output_file = open('./outlook_farming_001.csv','wb')    
output_writer = csv.writer(output_file, delimiter = ";", encoding='latin2')

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. 
messages = inbox.Items

for i, message in enumerate(messages):              # enumerated the items
    try:

        sender = message.SenderName                 
        sender_address = message.sender.address         
        sent_to = message.To                    
        date = message.LastModificationTime         
        subject = message.subject                   

        output_writer.writerow([
            date, 
            sender, 
            sender_address,             
            sent_to,
            subject]) 

    except Exception as e:
        ()

output_file.close()

问题:

  1. 如何确保摘录所有电子邮件? (当我运行脚本时,它可以工作,但它只提取了1555封电子邮件,虽然我的Outlook收件箱sais,它包含4785。)
  2. 如何使其适用于所有Outlook文件夹? (它只涉及收件箱,但我需要所有其他文件夹(已发送和其他已创建的文件夹))
  3. 如何获得收件人'电子邮件地址? (我只能提取经过筛选的名字)
  4. 如果您对任何问题有任何提示,我们将不胜感激。 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

问题2:

inbox = outlook.GetDefaultFolder(6)
代码中的

“6”表示收件箱。

for folder in outlook.Folders: 
    print(folder.Name)

使用上面的for循环来查找邮箱中的所有文件夹。

问题3:

要获取发件人电子邮件ID,您可以使用以下代码:

messages = inbox.Items
message = messages.GetFirst()
sender_emailid =message.SenderEmailAddress

问题1: 我没有答案。遗憾