Real Email Validation using Mailgun in Laravel
--
Email is one of the most preferred and effective communication channels, with most people checking their emails daily. Study said only 10% of email addresses collected are accurate which is mostly due to human error. As a developer or the owner of the system, email validation are one of the important things to do especially when the system have registration form.
Laravel already provides features with a wide options when it comes to email validation such as :-
- RFC Standard validation (rfc, strict)
- DNS Check Validation (dns)
- Spoof Check Validation (spoof)
- Email Filter Validation (filter)
By default, Laravel email validation doesn’t come with real email validation upfront. Even though we can send an email with OTP or links to verify, it would require the system to store the data temporarily in the database and mark it as unverified for a while.
Here come Mailgun Verification API. Mailgun Verification API will verify the given address based on:
- Mailbox detection
- Syntax checks (RFC defined grammar)
- DNS validation
- Spell checks
- Email Service Provider (ESP) specific local-part grammar (if available).
BUT, this API come with price. Pricing details for Mailgun’s email verification service can be found on our pricing page.
Here we will try to implement it in Laravel as the validation rule for the email. Before we started, it required you to register an account.
Lets get started
If we open the Mailgun api documentation,
We can trigger this verification API through GET request with /v3/address/validate
curl -G --user 'api:pubkey-XXXXXX' -G \
https://api.mailgun.net/v3/address/validate \
--data-urlencode address='foo@mailgun.net'
If you preferred POST request,
curl --user 'api:pubkey-XXXXX' -X POST
https://api.mailgun.net/v3/address/validate
-F address='foo@mailgun.net'