How to Build a Flask Email Verifier App

How to Build a Flask Email Verifier App

Introduction

In this tutorial, we will be looking at how to build an email-verifying application app using python. You will be able to know the integrity of the said email such as its disposability, last updated date, hosting provider, and much more.

Install modules

pip3 install Flask 

pip3 install requests

File Structure

Import modules

Now let’s create a file called app.py in the flask-email-verifier and import the modules shown below.

from flask import Flask, request, render_template
import requests

Initialize app

Now let’s initialize our flask application as shown below.

app = Flask(__name__)

Create view

@app.route('/',methods=['GET','POST'])
def index():
	if request.method == "POST":
		domain = request.form['domain']
		url = "https://mailcheck.p.rapidapi.com/"
		querystring = {"domain": domain}

		headers = {
			"X-RapidAPI-Key": "YOUR API KEY",
			"X-RapidAPI-Host": "mailcheck.p.rapidapi.com"
		}

		response = requests.request("GET", url, headers=headers, params=querystring).json()
		valid = response['valid']
		block = response['block']
		disposable = response['disposable']
		domain = response['domain']
		text = response['text']
		reason = response['reason']
		risk = response['risk']
		mx_host = response['mx_host']
		mx_ip = response['mx_ip']
		mx_info = response['mx_info']
		last_changed_at = response['last_changed_at']
		return render_template("index.html", valid=valid, block=block,
			disposable=disposable, domain=domain, text=text, 
			reason=reason, risk=risk, 
			mx_host=mx_host, mx_ip=mx_ip, 
			mx_info=mx_info, 
			last_changed_at=last_changed_at)
	return render_template("index.html")

if __name__ == "__main__":
	app.run(debug=True)

Create a template

Inside the template index.html we created earlier add the code below.

<!DOCTYPE html>
<html>
  <head>
    <title>Email Checker</title>
  </head>
  <body>
    <style>
        body {
        background-color: #1d1f21;
        color: #c5c8c6;
        font-family: 'Courier New', monospace;
        }
        h1 {
            text-align: center;
        }
        h1, h2, h3 {
        color: #f0c674;
        font-weight: bold;
        }

        form {
        margin-top: 50px;
        }

        label, input[type="submit"] {
        color: #c5c8c6;
        font-weight: bold;
        }

        input[type="text"] {
        background-color: #282a2e;
        color: #c5c8c6;
        border: 2px solid #373b41;
        border-radius: 5px;
        padding: 10px;
        }

        input[type="submit"] {
        background-color: #373b41;
        color: #c5c8c6;
        border: none;
        border-radius: 5px;
        padding: 10px 20px;
        margin-left: 10px;
        cursor: pointer;
        }

        .results {
        margin-top: 50px;
        }

        p {
        margin-top: 20px;
        }
    </style>
    <h1>Email Checker</h1>
    <form method="POST">
      <label for="domain">Enter a domain:</label>
      <input type="text" name="domain" id="domain">
      <input type="submit" value="Check">
    </form>
    {% if valid is not none %}
    <h2>Results:</h2>
    <p>Valid: {{ valid }}</p>
    <p>Block: {{ block }}</p>
    <p>Disposable: {{ disposable }}</p>
    <p>Domain: {{ domain }}</p>
    <p>Text: {{ text }}</p>
    <p>Reason: {{ reason }}</p>
    <p>Risk: {{ risk }}</p>
    <p>MX Host: {{ mx_host }}</p>
    <p>MX IP: {{ mx_ip }}</p>
    <p>MX Info: {{ mx_info }}</p>
    <p>Last Changed At: {{ last_changed_at }}</p>
    {% endif %}
  </body>
</html>

Run

Now let’s run our program. In your terminal run the command below.

python3 app.py 

Results

The above command if successfully executed should open the address [ http://127.0.0.1:8000 ]. Open your browser at the said address and your email verifying web app should appear as shown below.

There you have it, Thanks for reading. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *