[本文出自天外归云的博客园]
运行环境&前提条件: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
Original: https://www.cnblogs.com/LanTianYou/p/15994361.html
Author: 天外归云
Title: Python3.7读取outlook邮件内容——过滤出苹果应用商店审核未通过的邮件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15749/
转载文章受原作者版权保护。转载请注明原作者出处!