Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
django-email-validation
Advanced tools
Email verification for new signups or new users is a two-step verification process and adds a layer for security for valid users.
verify_email is a django app that provides this functionality right of the bat without any complex implementation.
This version contains breaking changes and is not compatible with the previous version 1.0.9
Features:
REQUEST_NEW_EMAIL_TEMPLATE
where user can specify his custom template for requesting email again. More on this here.email
.Read about this feature here
Bug Fixes:
Others
Note : The app is designed to be used right of the bat, however, further customizations options are also provided in Advance section below.
NOTE: Don't forget to activate the virtual environment if you have one.
pip install Django-Verify-Email
The steps to getting started are very simple. Like any other app, this can be installed easily by adding "verify_email" in your installed apps like:
Note: This documentation assumes that you already have a mail server configured for your project to send mails.
if not, then your first step should be Step 0:
--- Bypass this step if you already have these things set up for your project. ---
In your settings.py :
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_ID')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PW')
DEFAULT_FROM_EMAIL = 'noreply<no_reply@domain.com>'
Add "verify_email" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [
...
"verify_email.apps.VerifyEmailConfig",
]
Include the "verify_email" URLconf in your project urls.py like this:
urlpatterns = [
...
path('verification/', include('verify_email.urls')),
]
Apply migrations...
python manage.py migrate
For sending email from a signup form, in your views.py import:
...
from verify_email.email_handler import send_verification_email
Now in the function where you are validating the form:
...
def register_user(request):
...
if form.is_valid():
inactive_user = send_verification_email(request, form)
Attention : "send_verification_email()" takes two arguments, requests and form in order to set user's active status.
The "inactive_user" that is returned by "send_verification_email()" contains a saved user object just like form.save() would do(with is_active status set as False), which you can further use to extract user information from cleaned_data dictionary, as shown below :
inactive_user.cleaned_data['email']
# Output: test-user123@gmail.com
The user is already being saved as inactive and you don't have to .save() it explicitly.
If anything goes wrong in sending the verification link email, the user will not be saved, so that the user can try again.
Start the development server and signup with an email and you should be getting an email on the entered email with the default template for account activation. (You can provide your own HTML template. see Advance Section)
Note : The app comes with default email templates which can be overriden. See Custom Email Templates
That's right! , you don't have to implement any other code for validating users with their respective unique tokens and emails.
The app takes care of everything in the background.
path('verification/', include('verify_email')),
which you defined in your project's urls.py in step 2 above.
The link, by default, does not expire until it has been used at least once, however, you can change this behavior by specifying the time as "EXPIRE_AFTER" in settings.py. The variable can be set as :
Example
If I have to make a link expire after one-day, then I'd do:
If I have to make a link expire after one-hour, then I'd do:
If I have to make a link expire after one-minute, then I'd do:
Note: By default, if you do not specify a unit, it'll be considered in seconds.
A user can request a new verification link For a specific no. of times in case the previous one has expired. By default, a user can request new link two times which, obviously can be modified by you.
Set a "MAX_RETRIES" variable in settings.py specifying the no. of times a user is allowed to request a new link.
After that no. is exceeded, the user will be automatically redirected to an error page showing that you have maxed out.
In case when previous email/link is lost or deleted by the client, they can request a new email by specifying their email.
The path for that is https://yourdomain/verification/user/verify-email/request-new-link/
, at this path, there will be a form that will ask for the email of the registered user.
The pathname is request-new-link-from-email
which you can use to create a button on your front end and redirect traffic to the request email page.
Something like:
<a href="{% url 'request-new-link-from-email' %}">
This will redirect you to full path /verification/user/verify-email/request-new-link/
There are several checks done before sending an email again:
Then a new email will be sent to the given email.
The form template is supposed to be changed unless you are okay with the default template provided with the package.
To set your own custom template for form, set a variable name REQUEST_NEW_EMAIL_TEMPLATE
in settings.py with the path of template you want to use. Example:
REQUEST_NEW_EMAIL_TEMPLATE = 'mytemplates/mycustomtemplate.html'
and then your template will be displayed at the path.
Making Form: while making your custom template, keep in mind that the view will pass a variable named form
to the provided template, this form will contain only 1 field email
. Sample code that you can use while making your template is here:
<form method='POST' >
{% csrf_token %}
<fieldset>
{{form}}
</fieldset>
<div style="margin-top: 50px;">
<button class="btn btn-outline-info" type="submit">Request New Email</button>
</div>
</form>
You can apply your styles or whatever you want. (this code is used in the default template)
NOTE: This info is stored in the database so you have to apply migrations (step 3) to use this feature.
The app is packed with default HTML templates to handle the web pages but if you want to provide your own template you can do it by setting an attribute in settings.py :
HTML_MESSAGE_TEMPLATE = "path/to/html_template.html"
VERIFICATION_SUCCESS_TEMPLATE = "path/to/success.html"
VERIFICATION_FAILED_TEMPLATE = "path/to/failed.html"
REQUEST_NEW_EMAIL_TEMPLATE = "path/to/email.html"
LINK_EXPIRED_TEMPLATE = 'path/to/expired.html'
NEW_EMAIL_SENT_TEMPLATE = 'path/to/new_email_sent.html'
SUBJECT = 'subject of email'
# default subject is: Email Verification Mail
Two variables are passed in context dict of "HTML_MESSAGE_TEMPLATE" :
{{request}}
: Which is the same request passed in to send_verification_email.{{link}}
: Which contains verification linkIMPORTANT : if you are using custom html message template for email that has to be sent to user, provide a {{link}} as a template tag to contain verification link.
You Must Pass This In Your Template. Otherwise, the sent mail will not contain the verification link.
For Ex :
my_custom_email_message.html :
<div class="format-font" >
<a href="{{link}}" class="my-button" >Verify</a> # ----> The "link" variable is passed by the app's backend containing verification link.
</div>
----> "link" is a variable, that contains a verification link, and is passed in an HTML message template during sending the email to the user.
Success :
Two variables are passed in the context dictionary of "VERIFICATION_SUCCESS_TEMPLATE" :
{{mgs}}
: Which contains the message to be displayed on successful verification.{{link}}
: Which contains a redirect link to the login page.In template :
<h1 style="text-align: center; color: white;">
{{msg}} # __--> message variable
</h1>
<a href="{{link}}" class="btn btn-primary"> # __--> Link of login page
Login
</a>
Failed :
Only "{{msg}}" is passed for failed msg in the template.
In template :
<h1 style="text-align: center; color: white;">
{{msg}}
</h1>
After verification is successful, you might want to redirect the user to the login page. You can do this in two ways :
LOGIN_URL = 'name of your login pattern'
Note: This variable is also used by Django.
VERIFICATION_SUCCESS_TEMPLATE = None
There is always room for improvements and new ideas, feel free to raise PR or Issues
FAQs
A Django app for email verification.
We found that django-email-validation demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.