尝试打印从特定电子邮件地址收到的电子邮件数量

Trying to print amount of recieved emails from a specific email adress

我正在使用 exchangelib 库处理 python 中的一些代码,我正在尝试打印一个特定电子邮件地址的列表,其中包含一个计数器,该计数器计算从该电子邮件地址收到的电子邮件数量。这是我的代码,但我没有得到任何打印输出。

from exchangelib import Credentials, Account
from collections import defaultdict

emails = ['email1@example.com','email2@example.dk']
credentials = Credentials('random@user.dk', 'Brute')
account = Account('random@user.dk', credentials=credentials, autodiscover=True)




def count_senders(emails):
    counts = defaultdict(int)
    for email in emails:
        counts[email.sender.email_address] += 1
    return counts
    print(counts)
    

    
             

我在代码中看不到你的函数调用。也许这就是问题所在?

count_senders(emails)

其他问题是您在 return 之后打印,这不起作用。像这样更改函数顺序:

def count_senders(emails):
    counts = defaultdict(int)
    for email in emails:
        counts[item.sender.email_address] += 1
    print(counts)
    return counts

更新: 尝试将您的真实电子邮件和凭据放在 运行 代码之前。

emails = ['email1@example.com','email2@example.dk']

for email in emails:
    credentials = Credentials(email, 'Brute')
    account = Account(email, credentials=credentials, autodiscover=True)
    counts = defaultdict(int)
    for item in account.inbox.all().order_by('-datetime_received')[:100]:
        print(item.subject, item.sender, item.datetime_received)
        counts[email.sender.email_address] += 1
    print(counts)