-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_verification_code.py
44 lines (34 loc) · 1.47 KB
/
extract_verification_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import imaplib
import email
import re
# if you are using gmail you need to get an app password because gmail very strict about less secure apps.
# if you type here your real password gmail send authorization error.
def get_verification_code(email_address, email_password):
# Connect to the email server (adjust settings as needed)
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login(email_address, email_password)
mail.select("inbox")
# Search for the latest email
_, search_data = mail.search(None, "ALL")
latest_email_id = search_data[0].split()[-1]
# Fetch the email body
_, email_data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = email_data[0][1]
email_message = email.message_from_bytes(raw_email)
# Extract the verification code (adjust the regex as needed)
for part in email_message.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode()
# if your digits are different than 6, change the following code
match = re.search(r'\b(\d{6})\b', body)
if match:
verification_code = match.group(1)
verification_code_int = int(verification_code)
return verification_code_int
return None
def main():
mail = "" # your mail address
password = "" # your password and don't forget to enable imap
get_verification_code(mail, password)
if __name__ == '__main__':
main()