• Python3.7读取outlook邮件内容——过滤出苹果应用商店审核未通过的邮件


    [本文出自天外归云的博客园]

    运行环境&前提条件:Windows 10 安装 import 中提到的必要的 python 库

    代码如下:

    from datetime import datetime, timedelta
    import dateutil.parser
    import win32com.client
    import pytz
    
    target_account = "yourAccount@xx.com"
    target_folder = "收件箱"
    app_store_sender_target = "App Store Connect"
    review_not_pass_sig = "We identified one or more issues"
    
    
    def print_email_content(email):
        """
        打印邮件内容
        :param email:
        :return:
        """
        print(f"subject: {email.subject} | unread: {email.Unread}")
        print(f"from: {email.sendername} [{email.senderEmailAddress}]")
        print(f"received date and time: {email.receivedtime}")
        print(f"text: {email.body}")
    
    
    def filter_app_store_review_failed_mail_content(email):
        """
        过滤审核未通过邮件
        :param email:
        :return:
        """
        if app_store_sender_target not in email.subject:
            return None
        if app_store_sender_target not in email.sendername:
            return None
        if review_not_pass_sig not in email.body:
            return None
        return email
    
    
    def get_emails_latest(emails, duration):
        """
        获取近期邮件
        :param emails:
        :param duration:
        :return:
        """
        target_emails = []
        email = emails.GetFirst()
        now = pytz.UTC.localize(datetime.now())
        while emails:
            received_time = dateutil.parser.parse(str(email.receivedtime))
            email = emails.GetNext()
            if not email:
                break
            if now - timedelta(days=duration) < received_time:
                target_emails.append(email)
        print(f"filter emails in duration {duration} day(s): {len(target_emails)}")
        return target_emails
    
    
    class OutLook:
        def __init__(self):
            self.outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    
        def init_account_folders(self, account_name):
            for account in self.outlook.Folders:
                if str(account) == account_name:
                    print(f"choose account: {account}")
                    self.account_folders = account.Folders
    
        def get_emails_under(self, folder_name):
            for folder in self.account_folders:
                if str(folder) == folder_name:
                    emails = folder.Items
                    print(f"choose folder: {folder}({len(emails)})")
                    return emails
    
    
    if __name__ == '__main__':
        outlook = OutLook()
        outlook.init_account_folders(target_account)
        emails = outlook.get_emails_under(target_folder)
        latest_emails = get_emails_latest(emails, 1)
        review_not_passed_emails = []
        for email in latest_emails:
            if filter_app_store_review_failed_mail_content(email):
                review_not_passed_emails.append(email)
        print(f"review not passed emails count: {len(review_not_passed_emails)}")

    效果如下:

    参考资料:

    • https://www.linuxtut.com/en/8a285522b0c118d5f905/
    • https://stackoverflow.com/questions/5077625/reading-e-mails-from-outlook-with-python-through-mapi
  • 相关阅读:
    如何开始DDD(续)
    如何开始DDD
    ThinkNet终于见面了
    [Umbraco] umbraco中如何分页
    ETL 工具下载全集 包括 Informatica Datastage Cognos( 持续更新)
    js时间对比-转化为几天前,几小时前,几分钟前
    原生JS实现返回顶部和滚动锚点
    JSONP原理及简单实现 可做简单插件使用
    CSS3 transition效果 360度旋转 旋转放大 放大 移动
    js获取url的常用方法
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/15994361.html
Copyright © 2020-2023  润新知