Home > Backend Development > Golang > Email Verifier using Go

Email Verifier using Go

Linda Hamilton
Release: 2025-01-12 18:08:44
Original
411 people have browsed it

Email Verifier using Go

This blog post demonstrates building a simple email verification tool using Go. It's a mini-project designed to illustrate the core concepts of email verification and its underlying mechanisms. We won't delve into every detail, but we'll cover enough to provide a solid understanding.

The process begins with verifying the domain extracted from an email address (e.g., google.com).

Setting up your Go project:

Open your terminal and execute the following commands:

<code class="language-bash">1. go mod init github.com/username/email-verifier  // Replace 'username' with your GitHub username.
2. touch main.go</code>
Copy after login

Implementing the Go code (main.go):

The main.go file will contain the core logic. This initial snippet reads domain inputs from the command line:

<code class="language-go">package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        verifyDomain(scanner.Text())
    }

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

The verifyDomain function handles the actual verification process. It checks for MX, SPF, and DMARC records:

<code class="language-go">func verifyDomain(domain string) {
    var hasMX, hasSPF, hasDMARC bool
    var spfRecord, dmarcRecord string

    // MX Record Check
    mxRecords, err := net.LookupMX(domain)
    if err != nil {
        log.Printf("Error: could not find MX record for %s due to %v\n", domain, err)
    }
    if len(mxRecords) > 0 {
        hasMX = true
    }

    // SPF Record Check
    txtRecords, err := net.LookupTXT("spf." + domain)
    if err != nil {
        log.Printf("Error: could not find SPF record for %s due to %v\n", domain, err)
    }
    for _, record := range txtRecords {
        if strings.HasPrefix(record, "v=spf1") {
            hasSPF = true
            spfRecord = record
            break
        }
    }

    // DMARC Record Check
    dmarcRecords, err := net.LookupTXT("_dmarc." + domain)
    if err != nil {
        log.Printf("Error: could not find DMARC record for %s due to %v\n", domain, err)
    }
    for _, record := range dmarcRecords {
        if strings.HasPrefix(record, "v=DMARC1") {
            hasDMARC = true
            dmarcRecord = record
            break
        }
    }

    log.Printf("Domain: %v,\n MX: %v,\n SPF:  %v,\n DMARC:  %v,\n SPF Rec: %v,\n DMARC Rec %v,\n\n", domain, hasMX, hasSPF, hasDMARC, spfRecord, dmarcRecord)
}</code>
Copy after login

Explanation of Email Verification Components:

  • MX (Mail Exchanger) Record: Identifies the mail servers responsible for accepting email for a domain.
  • SPF (Sender Policy Framework) Record: Specifies authorized mail servers to send emails on behalf of a domain. Helps detect spoofing.
  • DMARC (Domain-based Message Authentication, Reporting & Conformance): Builds upon SPF and DKIM for enhanced email authentication and reporting. Specifies how to handle emails failing SPF or DKIM checks.

Running the code:

After saving the code, run it from your terminal: go run main.go. Enter domain names (e.g., google.com, example.com) one at a time. The output will show whether MX, SPF, and DMARC records were found.

Example Output:

<code>Domain: google.com,
 MX: true,
 SPF: false,
 DMARC: true,
 SPF Rec: ,
 DMARC Rec v=DMARC1; p=reject; rua=mailto:mailauth-reports@google.com,
</code>
Copy after login

This output indicates that the domain google.com has an MX record and a DMARC record, but the SPF record lookup failed in this example. The results will vary depending on the domain's DNS configuration.

Remember to replace "github.com/username/email-verifier" with your actual GitHub repository information. Connect with me on LinkedIn, GitHub, and Twitter/X for further discussion!

The above is the detailed content of Email Verifier using Go. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template