Sales: contact@spurqlabs.com

HR: hr@spurqlabs.com

  • Follow
  • Follow
  • Follow
  • Follow
  • Follow
SpurQLabs
  • Home
  • About
  • Blogs
  • Stories
  • Services
    • Functional Testing
    • Performance Testing
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

  • Top 5 UI Automation Tips for Flawless Testing
  • Best Practices for Writing Effective Test Cases
  • How Product Quality Builds Brand Loyalty in Marketing
  • Visual Testing: How to Verify Toggle Colors on Real Devices with Appium and Python
  • A Beginner’s Guide to Fast, Reliable Web Testing with CodeceptJS & Puppeteer 


Visit Us

US Office

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

UK Office

Regd. Office: 48 Wright crescent, Springfield, Chelmsford. CM1 6DP
Email: contact@spurqlabs.com
Contact No.: +44-0-7440029794

India Office

Regd. Office: Office No 201, Gera’s Imperium Rise, Hinjawadi Phase II, Pune, Maharashtra 411057
Email: contact@spurqlabs.com
Contact No.: +91-9356645806

Our Blogs

Top 5 UI Automation Tips for Flawless Testing

UI Automation Tips for Software Testing: UI automation is a very essential part of delivering high-quality applications at speed. But if the...
Read More

Best Practices for Writing Effective Test Cases

Writing effective test cases is crucial for ensuring software quality and reliability. A well-structured test case not only helps identify defects...
Read More

How Product Quality Builds Brand Loyalty in Marketing

Introduction to Marketing and Product Quality In today’s digital-first world, how a customer experiences your website, app, or product can make or...
Read More

Contact Us

All Rights Are reserved. Copyrights @2018 SpurQLabs Technologies Pvt. Ltd. I Privacy Policy I Terms Of Use

  • Follow
  • Follow
  • Follow
  • Follow
  • Follow