As you know Django’s forms.EmailField() is capable of validating email addresses but what if you wanted to validate the existence of a particular email address? I’ve found a pretty neat solution for that, it’s called Email Pie.
Email Pie is a wonderful little JSON API that gives you a simple way to validate email addresses. We’ll let you know if that email address is legit or not.. You could either use the service hosted on emailpie.com or run your own instance (it’s open source). In this post, I’m going to show you how to write a custom form field that makes use of Email Pie.
Make sure you pip install requests and simplejson before proceeding. Now create a fields.py file with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
validate_email_pie method makes GET request to Email Pie and gets back a response. The service checks for email format and mx records, it also catches misspellings (you can get it with response['didyoumean']). validate_email_pie raises ValidationError if the service detects an invalid email address. Now in your forms you can use EmailPieField like this:
1 2 3 4 5 6 | |
For those interested, here are some tests for the field :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |