Instagram
youtube
Facebook
  • 2 years, 1 month ago
  • 2813 Views

Email Verifier Tool using Golang

Yaman Birla
Table of Contents

Hii tech enthusiast, today we are going to build an Email Verifier Tool using GoLang. However, there are plenty of tools available if you look up them on google but they are quite expensive.

An email Verifier or email checker is a tool that validates an email address that exists and can receive emails in order to avoid bounces and safeguard your sender's reputation, verify before contacting new receivers. 

The email verifier connects to the SMTP server without sending an email.

This project is not very fancy, in the program, we are going to give the domain name and it will look at it using a net library and returns the MX record, SPF record, and DMARC record if it has one. 

Let us understand these fancy protocols that we used above in context:

MX (Mail exchange): “ The MX record indicates how email messages should be routed in accordance with the Simple Mail Transfer Protocol (SMTP, the standard protocol for all email)”

SPF: “An SPF record is a Sender Policy Framework record, the values are typically IP addresses and domain names. An SPF record is added to your domain provider in the form of a DNS TXT record”

DMARC: “Domain-based Message Authentication Reporting and Conformance (DMARC) is a free and open technical specification that is used to authenticate an email by aligning SPF and DKIM mechanisms.”

 

Now, let’s get started with the email verifier project

Firstly, you need to make a root directory for your project I have already made a root directory to store all of my projects in Go.

 

Make a file named email verifier in the command or manually

 

 

Now, start writing code in your preferred code editor:

 

 

 

Please, refer to the given code for demonstration:


package main

import (
	"fmt"
	"log"
	"bufio"
	"net"
	"os"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	fmt.Println("domain, hasMX, hasSPF, spfRecord, hasDMARC, dmarcRecord\n")

	for scanner.Scan(){
		checkDomain(scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		log.Fatal("Error: could not read from input: %v\n", err)
	}

}

func checkDomain(domain string) {

	var hasMX, hasSPF, hasDMARC bool
	var spfRecord, dmarcRecord string

	mxRecords, err := net.LookupMX(domain)

	if err!=nil {
		log.Println("Error: %v\n", err)
	}

	if len(mxRecords) > 0 {
		hasMX = true
	}

	txtRecords, err := net.LookupTXT(domain)

	if err!=nil {
		log.Println("Error: %v\n", err)
	}

	for _, record := range txtRecords{
		if strings.HasPrefix(record, "v=spf1") {
			hasSPF = true
			spfRecord = record
			break
		}
	}

	dmarcRecords, err := net.LookupTXT("_dmarc." + domain)

	if err != nil {
		log.Println("Error: %v\n", err)
	}

	for _, record := range dmarcRecords {
		if strings.HasPrefix(record, "v=DMARC1"){
			hasDMARC = true
			dmarcRecord = record
			break
		}
	}

	fmt.Println("%v, %v, %v, %v, %v, %v\n", domain, hasMX, hasSPF, spfRecord, hasDMARC, dmarcRecord)
}

 


 

We have imported many libraries here like net, os, strings, bufio, log, fmt which plays an important role in the successful establishment of this project.

Also, we made a checkDomain named function which handles all the important working of this code.

Now, let's understand the working of our code

 

 

Let's enter the domain name and wait for the results, it’d take some time to look for validation.

The output is in the format of

the output:

If the domain is techsolvo.com, codersdaily.in the results are

respectively.

 

GitHub: https://github.com/cosmic-weber/Golang/tree/main/email-verifier

 

Add a comment: