Introduction
In this tutorial, we will be building an email verifier app using Django. Here we will get details such as disposability, validity, and more about any domain, be it an email or a website address. For this project, we will be using Django, requests module to make HTTP requests, and an API from RapidApi.
Table of Contents
SEE ALSO: How to build a Django medical appointment application
Install modules
To begin let’s first install the modules above, to do so run the commands below on your terminal.
pip3 install Django
pip3 install requests
Project Configuration
To begin let’s first create our project, to do so run the command below on your terminal.
django-admin startproject django_email_verify
Now cd into the created folder and run the command below to create our application.
python3 manage.py startapp email_verify
Now open your project on a code editor and inside the settings.py in the django_email_verify folder add the following code inside the installed_apps list as shown below.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'email_verify',
]
SEE ALSO: How to build a Django movie app
Create a domain checker view
Now let’s create a view to check the domain. Inside the views.py file in the email_verify app add the code below.
from django.shortcuts import render
import requests
def index(request):
if request.method == "POST":
domain = request.POST.get('domain')
url = "https://mailcheck.p.rapidapi.com/"
querystring = {"domain": domain}
headers = {
"X-RapidAPI-Key": "f516ac8ed2msha55e4e0253b0e20p116b41jsn115b2b29fbb6",
"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']
context = {
'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(request, "email_verify/index.html", context)
return render(request, "email_verify/index.html")
In the code above we first take an API from RapidApi and subscribe and then we query our desired domain as shown above. Make sure to keep the API KEY secret after subscribing to the API.
Create templates
Now let’s create a template to request and render the email and the details. Inside the email_verify folder create a folder called templates and inside it create a folder called email_verify and create a file called index.html.
Inside the index.html file enter 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">
{% csrf_token %}
<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>
Create project route
Inside the email_verify folder create the route as shown below.
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name="index"),
]
Create project routes
Now inside our main project folder inside the b file let’s add the following code.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('email_verify.urls')),
]
Run
Now let’s start our server, to do so run the command below.
python3 manage.py runserver
After the command above executes successfully, we should see our app at the address [ http://127.0.0.1:8000 ]. Here is our app as shown below.
There you have it, Thanks for reading. Happy Coding.