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>
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>
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>
Explanation of Email Verification Components:
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>
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!