'How to read multiple gmail account in Python

I have a script that can read gmail inbox but only for a particular account. So, what i trying to achieve is to have a list to store multiple credentials and login to read inbox for multiple accounts. How can i achieve this goals with python ?

My script:

#email inbox declaration (receiver)
EMAIL_ACCOUNT = "[email protected]"
PASSWORD = "xxx"

#email
#Main function
class SendMail(threading.Thread):
    def __init__(self, id_manager):
        threading.Thread.__init__(self)
        self.id_manager = int(id_manager)

    def run(self):

        while True:
            mail = imaplib.IMAP4_SSL('imap.gmail.com')
            mail.login(EMAIL_ACCOUNT, PASSWORD)
            mail.list()
            mail.select('inbox')
            result, data = mail.uid('search', None, "UNSEEN")  # (ALL/UNSEEN)
            i = len(data[0].split())
            for x in range(i):
                latest_email_uid = data[0].split()[x]
                result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
                # result, email_data = conn.store(num,'-FLAGS','\\Seen')
                # this might work to set flag to seen, if it doesn't already
                raw_email = email_data[0][1]
                raw_email_string = raw_email.decode('utf-8')
                email_message = email.message_from_string(raw_email_string)

                # Header Details
                date_tuple = email.utils.parsedate_tz(email_message['Date'])
                if date_tuple:
                    local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
                    local_message_date = "%s" % (str(local_date.strftime("%a, %d %b %Y %H:%M:%S")))
                    email_from = str(email.header.make_header(email.header.decode_header(email_message['From'])))
                    email_to = str(email.header.make_header(email.header.decode_header(email_message['To'])))
                    global subject
                    subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))

                # Body details
                for part in email_message.walk():

                    if part.get_content_type() == "text/plain":
                        body = part.get_payload(decode=True)
                        print("From:", email_from)

                        print("Email To:", email_to)
                        print("date:", local_message_date)
                        print("Subject:", subject)
                        print("body:", body.decode('utf-8'))

                        #condition
                    else:
                        continue


def mainSendEmail():
    thread_id = ("0")
    led_index = 0
    thread_list = list()
    for objs in thread_id:
        thread = SendMail(led_index)
        thread_list.append(thread)
        led_index += 1
    for thread in thread_list:
        thread.start()
        time.sleep(1)



if __name__ == "__main__":
    mainSendEmail()

My list that store multiple credentials. listA is email and listB is the password. The listA's email and listB password is corelatted. For example, listA first email is listB's first password

listA = [('[email protected]',), ('[email protected]',), ('[email protected]',)]
listB = [('passwordxxx',), ('passwordForasd',), ('passwordFortest',)]



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source