简体   繁体   English

Outlook Python 中的附件条件

[英]Outlook Attachment Conditions in Python

Pretty new to Python. Python 的新手。 My goal is to download only email attachments from certain senders of.xls and.docx filetypes to a specified folder.我的目标是仅将 email 附件从 .xls 和 .docx 文件类型的某些发件人下载到指定文件夹。 I have the sender conditions working but can't get the program to filter to the specific filetypes I want.我有发件人条件,但无法让程序过滤到我想要的特定文件类型。 The code below downloads all attachments from the listed senders including image signatures (not desired.) The downloaded attachments contain data that will be further used in a df.下面的代码从列出的发件人那里下载所有附件,包括图像签名(不需要)。下载的附件包含将在 df.xml 中进一步使用的数据。 I'd like to keep it within win32com since I have other working email scraping programs that use it.我想将它保留在 win32com 中,因为我还有其他使用它的 email 抓取程序。 I appreciate any suggestions.我很感激任何建议。

Partially working code:部分工作代码:

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)

Items = inbox.Items
Item = Items.GetFirst()

def saveAttachments(email:object):
        for attachedFile in email.Attachments:
                try:
                        filename = attachedFile.FileName
                        attachedFile.SaveAsFile("C:\\Outputfolder"+filename)
                except Exception as e:
                        print(e)
for mailItem in inbox.Items:
        if mailItem.SenderName  == "John Smith" or mailItem.SenderName  == "Mike Miller":
                saveAttachments(mailItem)

Firstly, don't loop through all item in a folder - use Items.Find/FindNext or Items.Restrict with a query on the SenderName property - see https://docs.microsoft.com/en-us/office/vba/api/outlook.items.restrict首先,不要遍历文件夹中的所有项目 - 使用Items.Find/FindNextItems.Restrict查询SenderName属性 - 请参阅https://docs.microsoft.com/en-us/office/vba/ api/outlook.items.restrict

As for the attachment, a image attachment is not any different from any other attachment.至于附件,图像附件与任何其他附件没有任何不同。 You can check the file extension or the size.您可以检查文件扩展名或大小。 You can also read the PR_ATTACH_CONTENT_ID property (DASL name http://schemas.microsoft.com/mapi/proptag/0x3712001F ) using Attachment.PropertyAccessor.GetProperty and check if it is used in an img tag in the MailItem.HTMLBody property.您还可以使用Attachment.PropertyAccessor.GetProperty读取PR_ATTACH_CONTENT_ID属性(DASL 名称http://schemas.microsoft.com/mapi/proptag/0x3712001F )并检查它是否用于MailItem.HTMLBody属性中的 img 标记中。

Currently you save all attached files on the disk:当前,您将所有附加文件保存在磁盘上:

 for attachedFile in email.Attachments:
                try:
                        filename = attachedFile.FileName
                        attachedFile.SaveAsFile("C:\\Outputfolder"+filename)
                except Exception as e:
                        print(e)

only email attachments from certain senders of.xls and.docx filetypes to a specified folder.仅 email 来自某些 .xls 和 .docx 文件类型的发件人的附件到指定文件夹。

The Attachment.FileName property returns a string representing the file name of the attachment. Attachment.FileName属性返回一个表示附件文件名的字符串。 So, parsing the filename by extracting the file extension will help you to filter files that should be saved on the disk.因此,通过提取文件扩展名来解析文件名将帮助您过滤应该保存在磁盘上的文件。

Also you may be interested in avoiding hidden attachments used for inline images in the message body.此外,您可能有兴趣避免隐藏附件用于消息正文中的内联图像。 Here is an example code in VBA (the Outlook object model is common for all programming languages, I am not familiar with Python) that counts the visible attachments: Here is an example code in VBA (the Outlook object model is common for all programming languages, I am not familiar with Python) that counts the visible attachments:

Sub ShowVisibleAttachmentCount()
    Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
    Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"

    Dim m As MailItem
    Dim a As Attachment
    Dim pa As PropertyAccessor
    Dim c As Integer
    Dim cid as String

    Dim body As String

    c = 0

    Set m = Application.ActiveInspector.CurrentItem
    body = m.HTMLBody

    For Each a In m.Attachments
        Set pa = a.PropertyAccessor
        cid = pa.GetProperty(PR_ATTACH_CONTENT_ID)

        If Len(cid) > 0 Then
            If InStr(body, cid) Then
            Else
                'In case that PR_ATTACHMENT_HIDDEN does not exists, 
                'an error will occur. We simply ignore this error and
                'treat it as false.
                On Error Resume Next
                If Not pa.GetProperty(PR_ATTACHMENT_HIDDEN) Then
                    c = c + 1
                End If
                On Error GoTo 0
            End If
        Else
            c = c + 1
        End If
    Next a
    MsgBox c
End Sub

Also you may check whether the message body (see the HTMLBody property of Outlook items) contains the PR_ATTACH_CONTENT_ID property value.您还可以检查消息正文(参见 Outlook 项目的HTMLBody属性)是否包含PR_ATTACH_CONTENT_ID属性值。 If not, the attached can be visible to users if the PR_ATTACHMENT_HIDDEN property is not set explicitly.否则,如果未明确设置PR_ATTACHMENT_HIDDEN属性,则用户可以看到附件。

Also you may find the Sending Outlook Email with embedded image using VBS thread helpful.此外,您可能会发现使用 VBS 线程发送带有嵌入图像的 Outlook Email很有帮助。

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

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