Email validation is crucial for any application that collects user data. This guide covers the different approaches to email validation and how to implement it using a simple API.
Why Validate Emails?
Email validation helps you:
- Reduce bounce rates: Invalid emails hurt your sender reputation
- Improve deliverability: Clean lists get better inbox placement
- Save costs: Don't pay to send to invalid addresses
- Better UX: Catch typos before users submit
- Prevent abuse: Block obviously fake emails
Levels of Email Validation
Level 1: Syntax Validation
Check if the email follows the basic format: local@domain.tld
This catches obvious errors like missing @ symbols or invalid characters.
Level 2: Domain Validation
Verify that the domain exists and has valid MX records. This ensures the email can actually receive mail.
Level 3: Mailbox Validation
Check if the specific mailbox exists (SMTP verification). More accurate but slower and can trigger spam filters.
Using the Email Validation API
GET https://api.tinyfn.io/v1/validate/email?email=test@example.com
Headers: X-API-Key: your-api-key
{
"email": "test@example.com",
"is_valid": true,
"parts": {
"local": "test",
"domain": "example.com"
}
}
Implementation Examples
React Form Validation
async function validateEmail(email) {
const response = await fetch(
`/api/validate-email?email=${encodeURIComponent(email)}`
);
const data = await response.json();
return data.is_valid;
}
// In your form handler
const handleSubmit = async (e) => {
e.preventDefault();
const isValid = await validateEmail(email);
if (!isValid) {
setError('Please enter a valid email address');
return;
}
// Continue with form submission
};
Python Backend
import requests
def validate_email(email: str) -> bool:
response = requests.get(
'https://api.tinyfn.io/v1/validate/email',
params={'email': email},
headers={'X-API-Key': API_KEY}
)
return response.json()['is_valid']
# Usage
if not validate_email(user_email):
raise ValueError('Invalid email address')
Best Practices
- Validate on both client and server: Client-side for UX, server-side for security
- Don't be too strict: Valid email formats can be surprising (e.g.,
user+tag@domain.com) - Send confirmation emails: The only 100% validation is successful delivery
- Handle edge cases: International domains, new TLDs, etc.
- Cache results: Don't re-validate the same email repeatedly