Reputation: 333
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.
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',)]
Upvotes: 1
Views: 1166
Reputation: 1087
You can combine two lists with zip()
function to create a list of tuples. Each element in this new list contains a username and its password. Then you can iterate on this list to check all emails. Add these lines to the top of your run
function:
def run(self):
listA = [('[email protected]',), ('[email protected]',), ('[email protected]',)]
listB = [('passwordxxx',), ('passwordForasd',), ('passwordFortest',)]
for each_credential_info in tuple(zip(listA, listB)):
while True:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(each_credential_info[0][0], each_credential_info[1][0])
mail.list()
...
However, I'm wondering why you use tuples in listA
and listB
. You could use simple lists and write a simpler code:
def run(self):
listA = ['[email protected]', '[email protected]', '[email protected]']
listB = ['passwordxxx', 'passwordForasd', 'passwordFortest']
for each_credential_info in tuple(zip(listA, listB)):
while True:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(each_credential_info[0], each_credential_info[1])
mail.list()
...
And I have some more suggestions for your code to have better performance and ....
When you don't have a break in your infinite loop while True
, you won't have iteration on the new loop for each_credential_info in tuple(zip(listA, listB))
. In this case, you need to merge these two loops:
def run(self):
listA = ['[email protected]', '[email protected]', '[email protected]']
listB = ['passwordxxx', 'passwordForasd', 'passwordFortest']
credential_info = tuple(zip(listA, listB))
credential_info_index = -1
while True:
credential_info_index = (credential_info_index + 1) % len(credential_info)
print("Processing email: " + credential_info[credential_info_index][0] + ", " + credential_info[credential_info_index][1])
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(credential_info[credential_info_index][0], credential_info[credential_info_index][1])
mail.list()
...
Or simply make and use a circular itterator:
from itertools import cycle
def run(self):
listA = ['[email protected]', '[email protected]', '[email protected]']
listB = ['passwordxxx', 'passwordForasd', 'passwordFortest']
credential_info = cycle(tuple(zip(listA, listB)))
while True:
print("Processing email: " + next(credential_info)[0] + ", " + next(credential_info)[1])
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(next(credential_info)[0], next(credential_info)[1])
mail.list()
...
Upvotes: 1