email validation tools

How to Validate Email Addresses in Golang

Check if an email address is correct and really exists using the go programming language

Validation in a nutshell

An Email Address can look right but still be wrong and bounce. mailVALIDATION.io uses in depth email address validation to check if emails really exist without sending any messages.

Checking Formatting

You can use a Regular Expression to check if an Email Address is formatted correctly.

				
					package main

import (
	"fmt"
	"regexp"
)

func main() {
	pattern := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
	email := "foo@bar.com"
	matches := pattern.MatchString(email)
	fmt.Printf("email %v matches the pattern %v", email, matches)
}
				
			

But these the Email accounts might not actually exist and if you send an Email to them it might bounce.

Using API Key

With an API key you will be able to test lots of emails each day.

				
					package main

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

type RealEmailResponse struct {
    Status string `json:"status"`
}


func main() {
	email := "foo@bar.com"
	apiKey := // todo login to get api key
    jsonBody :[]byte('{"email": email}')
    bodyReader := bytes.NewReader(jsonBody)
	url := "https://app.mailvalidation.io/a/{team_slug}/validate/api/validate/"

	req, _ := http.NewRequest("POST", url, bodyReader)
	req.Header.Set("Authorization", "Api-Key " + apiKey)

	res, err := http.DefaultClient.Do(req)

	if err != nil {
	fmt.Println(err)
		return
	}

	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Printf("error %v", err)
		return
	}
	if res.StatusCode != 200 {
		fmt.Printf("unexpected result, check your api key. %v", res.Status)
		return
	}

	var myJson RealEmailResponse
	json.Unmarshal(body, &myJson)

	fmt.Printf("status for %v is %v", email, myJson.Status)
}
				
			

Bulk CSV File

Depending on your use case you may like to use the bulk email validation and read the csv file with go. You can validate a csv file full of emails addresses using the mailVALIDATION.

Get started with email address validations today.

Support

Are you having trouble, have some questions. How can we help?