Engineering

Advanced Email Validation using ApyHub’s API in Go

This tutorial shows how can you enhance email validation using Go, improving the overall quality and reliability of your email handling process.
Advanced Email Validation using ApyHub’s API in Go
SO
Sohail Pathan
Last updated on June 16, 2023
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.

How does it work?

Quick tutorial

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.

Prerequisites

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:
  1. To get started with ApyHub you first need to register for a free account to get an API Key (apy-token). You can use Google Authentication or GitHub Authentication as well.
image2.png
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.
Login-Screen.png
  1. Clone the project from the repository:
1
git clone https://github.com/iamspathan/apyhub-email-validation-tutorial-go
Or you can download the project from here.
  1. Add your Secret APY-TOKEN in main.go
1
func validateEmail(email string) (bool, error) {
2
url := "https://api.apyhub.com/validate/email/dns"
3
4
payload := struct {
5
Email string `json:"email"`
6
}{
7
Email: email,
8
}
9
10
jsonPayload, err := json.Marshal(payload)
11
if err != nil {
12
return false, err
13
}
14
15
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
16
if err != nil {
17
return false, err
18
}
19
20
if err != nil {
21
return false, err
22
}
23
// Add your apy-token here.
24
25
req.Header.Add("apy-token", "*********** ADD YOUR SECRET APY TOKEN HERE **************")
26
req.Header.Add("Content-Type", "application/json")
27
28
res, err := http.DefaultClient.Do(req)
29
if err != nil {
30
return false, err
31
}
32
defer res.Body.Close()
33
34
body, err := ioutil.ReadAll(res.Body)
35
if err != nil {
36
return false, err
37
}
38
39
var response struct {
40
Valid bool `json:"data"`
41
}
42
err = json.Unmarshal(body, &response)
43
if err != nil {
44
return false, err
45
}
46
47
return response.Valid, nil
48
}
  1. Now, We can start the server for making API calls. Use this command in terminal to start the server.
1
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.
  1. Finally, Navigate to http://localhost:3000, You will able to see the Signup page.
account-signup.png
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.

What more can you solve with this?

  1. Bulk email verification: Enterprises may have large email lists that need to be validated for accuracy and deliverability. An email validation API can be used to check the validity of each email address in the list, ensuring that emails are sent only to valid addresses.
  2. Fraud prevention: Email validation APIs can also be used to detect fraudulent email addresses used in phishing attacks, preventing unauthorised access to enterprise systems.
  3. Marketing campaigns: Email validation APIs can help enterprises ensure that their marketing emails reach the intended recipients by verifying the email addresses in their lists and removing invalid addresses.
  4. Compliance: Email validation APIs can help enterprises comply with data protection regulations by ensuring that emails are sent only to users who have provided valid consent.
  5. Customer support: Email validation APIs can be used to verify the email addresses of customers who submit support requests, ensuring that the enterprise is communicating with the correct customer.
So, if you're looking to improve email deliverability, reduce bounce rates and avoid spam accounts, Try out the API NOW ⬇️ DNS-Email-Validation-CTA.jpg