Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with this homework, using python 2.X Write a program that reads t

ID: 3879443 • Letter: P

Question

Please help me with this homework, using python 2.X

Write a program that reads through the mail box data and when you find a line that starts with “From”, extract the address information from the line. Count the number of messages from each person by using a dictionary. Note that you might need to look at more than “From” because of duplicate instances of the address (hint: “From “ vs. “From:”). Otherwise, embedded email “thread histories” may cause your count to be incorrect.

After all of the data has been read, print (i.e., print) the address of the person with the highest number of messages, along with the number of messages of that person. To do this, create a list of tuples (count, email) from the dictionary, sort the list in reverse order and print out the person who has the highest number of messages.

Explanation / Answer

Read Email From Using Python

Let’s iterate through the email and fetch the email with a particular Id. We’ll fetch the email using RFC822 protocol.
typ, data = mail.fetch(i, '(RFC822)' ) # i is the email id

Here is the full code for the Python utility to read emails from

import smtplib
import time
import imaplib
import email

# -------------------------------------------------
#
# Utility to read email from Using Python
#
# ------------------------------------------------

def read_email_from():
    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL,FROM_PWD)
        mail.select('inbox')

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.split()  
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])


        for i in range(latest_email_id,first_email_id, -1):
            typ, data = mail.fetch(i, '(RFC822)' )

            for response_part in data:
                if isinstance(response_part, tuple):
                    msg = email.message_from_string(response_part[1])
                    email_subject = msg['subject']
                    email_from = msg['from']
                    print 'From : ' + email_from + ' '
                    print 'Subject : ' + email_subject + ' '

    except Exception, e:
        print str(e)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote