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)}")

效果如下:

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

参考资料:

  • https://www.linuxtut.com/en/8a285522b0c118d5f905/
  • https://stackoverflow.com/questions/5077625/reading-e-mails-from-outlook-with-python-through-mapi

Original: https://www.cnblogs.com/LanTianYou/p/15994361.html
Author: 天外归云
Title: Python3.7读取outlook邮件内容——过滤出苹果应用商店审核未通过的邮件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15749/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部