Email validation is a crucial aspect of ensuring data accuracy and security, but relying solely on regular expression (regex) patterns for validation is insufficient. While regex can check the syntax of an email address and accept it if it matches the pattern, it does not guarantee the authenticity or validity of the email. This limitation opens the door for fake or disposable email addresses to enter the database, posing a vulnerability to your application. Additionally, considering these fake addresses as active users adds no value.
To address this problem, I have discovered a free online service provided by Apyhub that effectively solves the issue of email verification, providing a more reliable and secure solution.
ApyHub’s Email Validation API is a cloud-based API service that allows developers to easily integrate email validation functionality into their applications. This API is designed to validate email addresses in real time, providing accurate and reliable results that can help organisations reduce email bounce rates, prevent fraud, and improve their email marketing efforts.
In this tutorial, we will be going to build an authentication app using Mongoose for storing emails in a database. While the user signup, we will check whether the email is valid or not.
In this tutorial, we will be going to build an authentication app using Mongoose for storing emails in a database. While the user signup, we will check whether the email is valid or not.
We'll need access to:
Once your signup is successful, ApyHub creates a default application with APY-TOKEN. You can find the token by navigating to Dashboard → API Keys → Default Application Eg. APY*************************************************CZR
Once the API token is generated, We will check our DNS Email API documentatioan for integration.
git clone https://github.com/iamspathan/apyhub-email-validation-tutorial-go
Or you can download the project from here.
func validateEmail(email string) (bool, error) {
url := "https://api.apyhub.com/validate/email/dns"
payload := struct {
Email string `json:"email"`
}{
Email: email,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return false, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
return false, err
}
if err != nil {
return false, err
}
// Add your apy-token here.
req.Header.Add("apy-token", "*********** ADD YOUR SECRET APY TOKEN HERE **************")
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return false, err
}
var response struct {
Valid bool `json:"data"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return false, err
}
return response.Valid, nil
}
go run main.go
Tip: The go run
command is used in Go to compile and run a Go program in a single step. It allows you to quickly execute a Go source file without explicitly building an executable binary.
Note: The authentication module mentioned in this project is provided solely as a recommendation for the project structure. While we highly encourage its use and integration according to your architecture pattern.
We just saw, how the email validation process will only allow the verified email to create an account. But there are several use cases in which Advanced Email Validation fits.
So, if you're looking to improve email deliverability, reduce bounce rates and avoid spam accounts, sign up for ApyHub's email validation API today.