defget_security_code():
security_code=""condition=Truecur_time=datetime.datetime.now()
timeout=time.time() +30# wait for receiving new mailwhilecondition:
iftime.time() >timeout:
print('check mail timeout,not received verify code from mail')
breakresult, data=mail.uid('search', None, "ALL") # (ALL/UNSEEN)# i = len(data[0].split())latest_email_uid=data[0].split()[-1]
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 alreadyraw_email=email_data[0][1]
raw_email_string=raw_email.decode('utf-8')
email_message=email.message_from_string(raw_email_string)
# Header Detailsdate_tuple=email.utils.parsedate_tz(email_message['Date'])
ifdate_tuple:
local_date=datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
iflocal_date>=cur_time:
condition=Falsebreak# Get title ifnotcondition:
security_code=email_message['subject'].split()[0]
print(security_code)
returnsecurity_code