Django · Python
Block disposable email in Django
Add a form validator that calls Piisend before saving the user. Works in Django 3, 4, and 5. Pairs cleanly with django-allauth or Django's built-in auth.
The code
# validators.py
import os
import httpx
from django.core.exceptions import ValidationError
CHECK_URL = "https://api.piisend.com/api/v1/check/email"
def reject_disposable_email(value: str) -> None:
try:
r = httpx.get(
f"{CHECK_URL}/{value}",
headers={
"Authorization": f"Bearer {os.environ['PIISEND_API_KEY']}",
},
timeout=5.0,
)
if not r.is_success:
return # fail open
# reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
if r.json().get("recommendation") == "block":
raise ValidationError("Please use a permanent email address.")
except httpx.HTTPError:
return # fail open
# forms.py
from django import forms
from .validators import reject_disposable_email
class SignupForm(forms.Form):
email = forms.EmailField(validators=[reject_disposable_email])
password = forms.CharField(widget=forms.PasswordInput)API key
Store `PIISEND_API_KEY` in environment variables or Django settings — never commit live keys.
Fail-open vs fail-closed
Network errors silently pass validation. Log failures if you need to monitor API health.
Pair with verification
Call the validator in `UserCreationForm.clean_email()` or allauth's adapter `clean_email` hook.
FAQ
Where does the Piisend API key go in Django?
Store `PIISEND_API_KEY` in environment variables or Django settings — never commit live keys.
What happens if Piisend is slow or down?
Network errors silently pass validation. Log failures if you need to monitor API health.
How does this fit with the rest of Django signup?
Call the validator in `UserCreationForm.clean_email()` or allauth's adapter `clean_email` hook.
What does this snippet block?
Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.
Can I get a full allow / review / block verdict instead?
Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.
Send verification email with Piisend
After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.