Skip to content

Using Go to Validate and Block Disposable Emails

This document provides example code for how to use Go to validate an email and determine if it is from a disposable email provider using the Check-Mail API.

Validating and Blocking Disposable Emails with Go

Example Go Code

The following function uses Go to validate an email address by making an HTTP GET request to the Check-Mail API.

package main

import (
   "encoding/json"
   "fmt"
   "io/ioutil"
   "net/http"
)

// Function to verify an email address
func verifyEmail(email string) (map[string]interface{}, error) {
   // Set your API key
   apiKey := "YOUR_API_KEY"

   // Construct the API URL
   url := fmt.Sprintf("https://api.check-mail.org/v1/?email=%s", email)

   // Create a new HTTP request
   req, err := http.NewRequest("GET", url, nil)
   if err != nil {
      return nil, err
   }

   // Set headers
   req.Header.Set("Authorization", "Bearer "+apiKey)
   req.Header.Set("Content-Type", "application/json")

   // Perform the request
   client := &http.Client{}
   resp, err := client.Do(req)
   if err != nil {
      return nil, err
   }
   defer resp.Body.Close()

   // Read the response body
   body, err := ioutil.ReadAll(resp.Body)
   if err != nil {
      return nil, err
   }

   // Parse the JSON response
   var responseData map[string]interface{}
   err = json.Unmarshal(body, &responseData)
   if err != nil {
      return nil, err
   }

   return responseData, nil
}

// Example usage
func main() {
   email := "fred@cbaccounts.com"
   responseData, err := verifyEmail(email)
   if err != nil {
      fmt.Println("Error:", err)
      return
   }

   // Output the response
   fmt.Printf("Email: %s\n", email)
   fmt.Printf("Valid: %t\n", responseData["valid"])
   fmt.Printf("Block: %t\n", responseData["block"])
   fmt.Printf("Disposable: %t\n", responseData["disposable"])
   fmt.Printf("Risk Score: %v\n", responseData["risk"])
}

Replace YOUR_API_KEY with your actual API key and fred@cbaccounts.com with the email address you want to validate.

Explanation

  • The verifyEmail function: Sends a GET request to the Check-Mail API, parses the JSON response, and returns it as a map.
  • Error handling is included to handle issues with the request or response.
  • The response data is printed to the console, showing if the email is valid, if it should be blocked, if it's disposable, and the risk score.

Next Steps

  • Integrate this Go function into your application to automate the detection of disposable email addresses.
  • Refer to the documentation for further details on the API.