Sales: contact@spurqlabs.com

HR: hr@spurqlabs.com

  • Follow
  • Follow
  • Follow
  • Follow
  • Follow
SpurQLabs
  • Home
  • About
  • Blogs
  • Stories
  • Services
    • Functional Testing Services
      • Functional Testing
      • System Integration Testing (SIT)
      • Regression Testing
      • User Acceptance Testing
      • Smoke Testing
      • Sanity Testing
      • Exploratory Testing
      • Cross-Platform Testing
    • Test Automation Services
      • Web Test Automation
      • API Test Automation
      • Desktop Application Test Automation
      • Cross-Platform Test Automation
      • CI/CD Integration for Test Automation
Book A Demo
How to fetch a Link and OTP from Email Using Python and Selenium?

How to fetch a Link and OTP from Email Using Python and Selenium?

by Harishchandra Ekal | Jun 4, 2024 | Blog

In this Blog, I’ll walk you through the process of fetching an email link and OTP from Email using Python. Learn how to fetch links & OTP from email efficiently with simple steps. Email and OTP (One-Time Password) verification commonly ensure security and verify user identity in various scenarios.

Some typical scenarios include:

  • User Registration
  • Password Reset
  • Two-factor authentication (2FA)
  • Transaction Verification
  • Subscription Confirmation

We’ll leverage the imap_tools library to interact with Gmail’s IMAP server. We’ll securely manage our credentials using the dotenv library. This method is efficient and ensures that your email login details remain confidential.

Step-by-Step Instructions

Set Up Your Environment to Fetch OTP from Email:

  • Ensure you have Python installed on your system.
  • Install the required libraries by running:
    • pip install python-dotenv
    • https://pypi.org/project/python-dotenv/
    • imap-tools
    • https://pypi.org/project/imap-tools/

Store Credentials Securely to Fetch OTP from Email:

A .env file is typically used to store environment variables. This file contains key-value pairs of configuration settings and sensitive information that your application needs to run but which you do not want to hard-code into your scripts for security and flexibility reasons.

Create a .env file in your project directory to store your Gmail credentials.

  • EMAIL_USER=your-email@gmail.com
  • EMAIL_PASS=your-password

How to Create and Use an App Password for Gmail

  • To securely fetch emails using your Gmail account in a Python script, you should use an App Password.
  • This is especially important if you have two-factor authentication (2FA) enabled on your account.
  • Here’s a step-by-step guide on how to generate an App Password in Gmail:
Manage Google Account
  • Go to your Google Account settings.
  • Select “Security” from the left-hand menu.
fetch otp from email

Enable Two-Factor Authentication:

  • Go to your Google Account Security Page.
  • Under the “Signing in to Google” section, ensure that 2-Step Verification is turned on. If it’s not enabled, click on “2-Step Verification” and follow the instructions to set it up.
  • Generate an App Password:
  • Once 2-step Verification is enabled, return to the Google Account Security Page.
  • Under the “Signing in to Google” section, you will now see an option for “App passwords.” Click on it.
2-Step Verification
  • You might be prompted to re-enter your Google account password.
fetch otp from email
  • In the “Select app” dropdown, choose “Mail” or “Other (Custom name)” and provide a name (e.g., “Python IMAP”).
  • In the “Select device” dropdown, choose the device you’re generating the password for, or select “Other (Custom name)” and enter a name (e.g., “My Computer”).
  • Click on “Generate.”
  • Google will provide you with a 16-character password. Note this password down securely, as you’ll need it for your Python script.

Load Environment Variables:

In your Python script, use the dotenv library to load these credentials securely. Here’s how you can do it:

from dotenv import load_dotenv
from imap_tools import MailBox, AND
import os
# Load .env file
load_dotenv()
# Read variables
email_user = os.getenv('EMAIL_USER')
email_pass = os.getenv('EMAIL_PASS')

Loading Environment Variables:

The dotenv library is used to load the email username and password from the .env file. This approach keeps your credentials secure and out of your source code.

Connect to Gmail and Fetch Emails:

We will create a function to connect to Gmail’s IMAP server and fetch the latest unread email. The function will look like this:

def check_latest_email():
    # Connect to Gmail's IMAP server
    with MailBox('imap.gmail.com').login(email_user, email_pass, 'INBOX') as mailbox:
        # Fetch the latest unread email
        emails = list(mailbox.fetch(AND(seen=False), limit=1, reverse=True))

        if len(emails) == 0:
            return None, None, None  # No Emails Found
        return emails[0]
if __name__ == "__main__":
    email = check_latest_email()
    if email:
        print("Email subject: ", email.subject)
        print("Email text: ", email.text)
        print("Email from: ", email.from_)
    else:
        print("No new emails found.")

Connecting to Gmail’s IMAP Server:

  • Using the imap_tools library, we connect to Gmail’s IMAP server.
  • The MailBox class handles the connection.
  • The login method authenticates using your email and password.

Fetching the Latest Unread Email:

  • The fetch method retrieves emails based on specified criteria.
  • AND(seen=False) ensures we only get unread emails.
  • limit=1 fetches the latest one.
  • reverse=True sorts the emails in descending order.

Handling Email Data:

  • The function check_latest_email returns the most recent unread email’s subject, text, and sender.
  • If no new emails are found, it returns None.
  • By following these steps, you can efficiently fetch the latest unread email from your Gmail inbox using Python.
  • This method is not only secure but also straightforward, making it easy to integrate into your projects.

Fetching the link from email:

def extract_link(email_text):
    # Regex pattern to match URLs
    url_pattern = re.compile(r'https?://[^\s]+')
    match = url_pattern.search(email_text)
    if match:
        return match.group()
    return None
#Example to fetch link from email content:
link = extract_link(email.text)
        if link:
            print("Extracted Link: ", link)
        else:
            print("No link found in the email content.")

Fetching OTP from email:

Create a function to extract the OTP from the email content using a regular expression. This assumes the OTP is a 6-digit number, which is common for many services:

def extract_otp(email_text):
    # Regex pattern to match a 6-digit number
    otp_pattern = re.compile(r'\b\d{6}\b')
    match = otp_pattern.search(email_text)
    if match:
        return match.group()
    return None
#Example to extract otp from email
otp = extract_otp(email.text)
        if otp:
            print("Extracted OTP: ", otp)
        else:
            print("No OTP found in the email content.")

Refer to the following GitHub repository for instructions on how to fetch links and OTPs from Gmail.

  • https://github.com/hekal-spurqlabs/FetchOTP

Conclusion:

Fetching links and OTPs from email content programmatically is essential for enhancing security, improving user experience, and increasing operational efficiency. Automation ensures timely and accurate processing, reducing the risk of errors and phishing attacks while providing a seamless user experience. This approach allows businesses to scale their operations, maintain compliance, and focus on strategic activities.

Click here for more blogs on software testing and test automation.

Harishchandra Ekal
Harishchandra Ekal

Harish is an SDET with expertise in API, web, and mobile testing. He has worked on multiple Web and mobile automation tools including Cypress with JavaScript, Appium, and Selenium with Python and Java. He is very keen to learn new Technologies and Tools for test automation. His latest stint was in TestProject.io. He loves to read books when he has spare time.

Recent Posts

  • Smart way of E2E testing using cypress new features cy.prompt
  • Boosting Web Performance: Integrating Google Lighthouse with Automation Frameworks
  • Top 10 tips to debug your Java code with IntelliJ
  • 10 Prompting Secrets Every QA Should Know to Get Smarter, Faster, and Better Results
  • 7 Common Software Testing Mistakes and How to Fix Them Using AI


US Office

11097, Paisano Dr, Frisco, Texas, 75035
Contact No –  +1-469-915-4206
Email Id – contact@spurqlabs.com

UK Office

48 Wright crescent, Springfield, Chelmsford. CM1 6DP
Contact No. – +44-7576772275
Email Id – contact@spurqlabs.com

India Office

Office No. 201, Gera’s Imperium Rise, Hinjewadi Phase-2, Pune, Maharashtra. Pin Code – 411057
Contact No. – +91 9356645806
Email Id – contact@spurqlabs.com

Smart way of E2E testing using cypress new features cy.prompt

How to use cy.prompt in Cypress; this blog introduces cy.prompt, an experimental tool from Cypress designed to simplify web automation by allowing users to write tests using natural language descriptions rather than complex CSS selectors. By leveraging artificial...
Read More

Boosting Web Performance: Integrating Google Lighthouse with Automation Frameworks

The Silent Killer of User Experience Integrating Google Lighthouse with Playwright; Picture this: Your development team just shipped a major feature update. The code passed all functional tests. QA signed off. Everything looks perfect in staging. You hit deploy with...
Read More

Top 10 tips to debug your Java code with IntelliJ

In today’s fast-paced development world, debugging can easily become a dreaded task, so here is the complete guide to Debugging Java code in IntelliJ. You write what seems like perfect code, only to watch it fail mysteriously during runtime. Furthermore, maybe a...
Read More

10 Prompting Secrets Every QA Should Know to Get Smarter, Faster, and Better Results

The Testing Skill Nobody Taught You Here's a scenario that plays out in QA teams everywhere: A tester spends 45 minutes manually writing test cases for a new feature. Another tester, working on the same type of feature, finishes in 12 minutes with better coverage,...
Read More

7 Common Software Testing Mistakes and How to Fix Them Using AI

Software testing mistakes to fix using AI — software testing isn’t just about finding bugs — it’s about ensuring that the product delivers value, reliability, and confidence to both the business and the end-users. Yet, even experienced QA engineers and teams fall into...
Read More

All Rights Are Reserved. Copyright@2026 SpurQLabs Technologies Pvt. Ltd. | Privacy Policy | Term of Use