Search code examples
pythonanalysisexchangelib

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


I'm working with some code in python with the exchangelib library, I'm trying to print a list of specific email addresses with a counter that counts the amount of emails recieved from that emailaddress. This is my code, but I'm not getting any printed output.

from exchangelib import Credentials, Account
from collections import defaultdict

emails = ['[email protected]','[email protected]']
credentials = Credentials('[email protected]', 'Brute')
account = Account('[email protected]', 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)
    

    
             

Solution

  • I can't see your function call in the code. Maybe that is the problem?

    count_senders(emails)
    

    Other problem is that you are printing after the return which doesn't work. Change your function order like this:

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

    Update: Try to put your real email and credentials before running the code.

    emails = ['[email protected]','[email protected]']
    
    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)