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

问题描述

我正在使用带有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)

另一个问题是您无法在返回之后进行打印。像这样更改功能顺序:

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)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...