If you use email marketing for your business or send emails to mailing lists, you are probably familiar with the challenge of having bad or fake email addresses in your database.
Every time you send a bad email, you receive a notification that your message has been rejected, which is more than just an annoyance and represents a significant risk.
Service providers check your bounce rate. If they find that you are sending too many emails to the wrong addresses, they may blacklist you. This can have even greater implications than spam filtering, as many of your recipients' mail servers could reject every single mail you send. Once your servers are blacklisted, it can be complicated to remove the blacklisting.
You can verify your server's blacklisting status on a service like Debouncer.
As a developer, you are looking to implement the best way to validate an email address format. To do so, you first need to understand precisely what format is allowed by Internet Standards.
Section 3,2,4 of RFC 2822 specifies that the local string (the part of the email address that comes before the @) can contain the following characters: ! $ & * - = ` ^ | ~ # % ' + / ? _ { }
According to this, validation of an email address format requires quite a complex mechanism. Luckily the URI::MailTo class, which is part of the Ruby Standard Library, contains the URI::MailTo::EMAIL_REGEXP constant, which can be used for email format validation, so you don't have to create your own.
However, according to the next section of RFC 2822, an email address can contain any characters as long as quotes escape them. As an example, this is a correct email address:
And the URI::MailTo::EMAIL_REGEXP constant would reject it.
At this point, you could conclude that URI::MailTo::EMAIL_REGEXP is able to validate 99% of all emails submitted to your forms, which may be good enough.
But an address like sefgqpouinqlzoiyhqjhzgf@gmail.com has a correct syntax and would be validated by any regular expression you may implement. Chances are, though, that the mailbox does not exist, and every email sent to this address would bounce back.
An efficient email checker would not only verify the address format but also verify if the email is hosted by a free or paid service, if the domain correctly contains an MX record and if it points to a valid SMTP server, if the email domain is linked to a disposable email service, and more.
Abstract is a fast and reliable API provider that offers multiple services, including email validation. Once subscribed, you get free access to the API along with your private API key.
Abstract Email Validation API is accessible through a GET request and takes 2 arguments. Here is its format:
Using Net::HTTP, here is how to make the request and retrieve the response:
The response is in JSON format and contains multiple parameters:
Here is an example of a response: