i-search接收outlook郵件時主題及附件的多條件過濾

在使用outlook接收郵件的時候,咱們可能會遇到這種狀況,須要對主題或者附件進行過濾,可是過濾條件可能不止一個,這時RPA的ioutlook函數,可能不是很好使,因此本身就根據源碼對過濾進行了簡單的升級和優化。python

from ubpa import ioutlook
from ubpa import iresult
import tempfile
import os

__attachment_path = tempfile.gettempdir() + os.sep  # 默認保存路徑


def recv_outlook(mail_account, mail_inbox='收件箱', sender_filter=None, subject_filter=None, attachments_filter=None,
                 attachment_path=None, mark_as_read=True, only_unread=True, top=1):
    '''
    :param mail_account: 郵箱帳號
    :param mail_inbox: 收件箱名稱
    :param sender_filter 發件人過濾條件
    :param subject_filter: 主題過濾條件, list類型
    :param attachments_filter: 附件過濾條件, list類型
    :param attachment_path: 附件路徑
    :param mark_as_read: 標爲已讀郵件
    :param only_unread: 查找未讀郵件
    :param top: 一次讀取幾封郵件
    :return: tuple(郵件對象列表,郵件狀態, 附件列表)
    '''
    # 郵件列表
    mails = []
    num = 1
    file_names = []
    state = False

    inbox = ioutlook.get_inbox(mail_account, mail_inbox)
    if inbox:
        messages = inbox.Items

        if only_unread:
            messages = messages.Restrict('[UnRead] = True')  # 未讀取的郵件

        messages.Sort("[ReceivedTime]", True)  # 收件時間倒序

        if not top:
            top = len(messages)

        # 循環未讀郵件
        for message in messages:
            # 附件列表
            atts = []
            if num > top:
                break

            sender = message.SenderName  # 發件人名字
            sender_addr = message.SenderEmailAddress  # 發件人地址
            subject = message.Subject  # 主題
            body = message.Body  # 正文
            cc = message.CC  # 抄送
            received_time = str(message.ReceivedTime)  # 收件時間
            attachments = message.Attachments  # 附件

            # 發件人過濾
            if sender_filter and sender_filter not in sender:
                continue
            else:
                state = True

            # 主題過濾
            if subject:
                if subject_filter:
                    for sub in subject_filter:
                        if sub and sub in subject:
                            state = True
                # 無過濾條件時
                else:
                    state = True
            else:
                state = None
                # 無主題時將郵件標記爲已讀
                if mark_as_read:
                    message.UnRead = False
                continue

            # 附件過濾
            if len(attachments) > 0:
                for attachment in attachments:
                    file_name = attachment.FileName
                    file_names.append(file_name)
                    k = 0
                    # 有過濾條件
                    if attachments_filter:
                        for att in attachments_filter:
                            if att and att in file_name:
                                state = True
                                if not attachment_path:
                                    attachment_path = __attachment_path
                                att_file_path = attachment_path + os.sep + file_name
                                attachment.SaveAsFile(att_file_path)
                                atts.append(att_file_path)
                                k += 1
                            else:
                                if k == 0:
                                    state = False
                                    # 無附件將郵件標記爲已讀
                                    if mark_as_read:
                                        message.UnRead = False
                    # 無過濾條件下載全部附件
                    else:
                        if not attachment_path:
                            attachment_path = __attachment_path
                        att_file_path = attachment_path + os.sep + file_name
                        attachment.SaveAsFile(att_file_path)
                        atts.append(att_file_path)
                        k += 1
            else:
                # 無附件將郵件標記爲已讀
                if mark_as_read:
                    message.UnRead = False
                continue
            if state:
                mail_message = iresult.MailMessage()  # 實例化

                # 屬性賦值
                mail_message.sender = sender
                mail_message.sender_mail = sender_addr
                mail_message.received_time = received_time
                mail_message.subject = subject
                mail_message.body = body
                mail_message.cc = cc
                mail_message.attachments = atts

                mails.append(mail_message)

                num += 1

                if mark_as_read:
                    message.UnRead = False  # 標誌爲已讀
    return mails, state, file_names


res = recv_outlook('test', subject_filter=['test', '測試'], sender_filter='test', attachments_filter=['測試', '小米'],
                   attachment_path=r"C:\Users\Desktop")

以上代碼的返回值,能夠作如下處理app

if res[0]:
    print('準備執行程序。。。')
else:
    if res[1]:
        print('主題不符')
    else:
        if res[1] == None:
            print('郵件無主題')
        else:
            if res[2]:
                print('郵件附件格式不正確')
            else:
                print('未收到有效郵件')
相關文章
相關標籤/搜索