我们发送的电子邮件数量(Outlook)-python代码[关闭]

问题描述 投票:0回答:1

我需要一个 Python 代码来计算一个月/一年内发送到特定目的地的所有电子邮件。你能帮忙吗?

python email outlook numbers counting
1个回答
0
投票

是的,我可以帮你。下面是一个示例 Python 代码,它使用 IMAP 库连接到电子邮件帐户并计算给定月份/年份发送到特定目的地的电子邮件数量:

import imaplib
import email
import datetime

# Set the email account credentials and IMAP server
username = 'your_email_address'
password = 'your_password'
imap_server = 'imap.gmail.com'

# Set the email destination and the month/year to count emails for
email_destination = 'destination_email_address'
month = '04' # example month in MM format
year = '2023' # example year in YYYY format

# Connect to the email account using IMAP
imap = imaplib.IMAP4_SSL(imap_server)
imap.login(username, password)

# Select the inbox folder
imap.select('inbox')

# Set the search criteria based on the email destination and month/year
search_criteria = f'(TO "{email_destination}") (SINCE "{year}-{month}-01" BEFORE "{year}-{month}-31")'

# Search for emails using the search criteria
status, email_ids = imap.search(None, search_criteria)

# Get the number of emails returned by the search
email_count = len(email_ids[0].split())

# Print the number of emails sent to the specified destination for the given month/year
print(f'Total emails sent to {email_destination} in {month}/{year}: {email_count}')

# Close the IMAP connection
imap.close()
imap.logout()

注意:此示例假设您使用 Gmail 帐户作为电子邮件提供商,并且您已为您的帐户启用 IMAP 访问。如果您使用不同的电子邮件提供商,您可能需要相应地调整 IMAP 服务器和登录凭据。

© www.soinside.com 2019 - 2024. All rights reserved.