Sitemap
Amal Mullangath

Amal Mullangath

Building an Email Verification System with Go (Part-1)

3 min readJan 29, 2024

--

Press enter or click to view image in full size

Introduction

Welcome, tech enthusiasts! In this blog, we explore building an Email Verification System using Go. This project focuses on validating the existence and format of email addresses. From Go coding to Docker containerization and Azure deployment, we cover it all.

The Code: Exploring main.go

Web Form Interface

Our journey begins with a web form interface, created using Go’s html/template package. This form allows users to input an email address for verification.

const formTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>Email Domain Verifier</title>
</head>
<body>
...
<form method="post" action="/verify">
Email: <input type="email" name="email">
<input type="submit" value="Verify">
</form>
...
</body>
</html>
`

Email Verification Logic

We use regular expressions to split the email at the ‘@’ symbol. The MX record lookup is performed using net.LookupMX.

parts := regexp.MustCompile(`@`).Split(email, 2)
_, err := net.LookupMX(parts[1])
return err == nil

Containerization with Docker

We containerize our application using a Dockerfile. Key steps include setting the base image, working directory, copying files, building the app, exposing ports, and running the app.

FROM golang:1.20.4
WORKDIR /app
COPY . /app
RUN go build -o main .
EXPOSE 8080
CMD ["/app/main"]

Deploying on Azure

Deploying the container on Azure involves several steps:

  1. VM Creation: Setting up a Linux VM on Azure.
  2. Docker Deployment: Pulling and running the Docker container on the VM.
  3. Domain Configuration: Updating A records to point to the VM’s IP.

Full main.go Code

Here is the complete main.go code for your reference:

package main
import (
"html/template"
"log"
"net"
"net/http"
"regexp"
)
const formTemplate = `...`func validateEmailDomain(email string) bool {
parts := regexp.MustCompile(`@`).Split(email, 2)
if len(parts) != 2 {
return false
}
_, err := net.LookupMX(parts[1])
return err == nil
}
func handler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("form").Parse(formTemplate))
if r.Method != http.MethodPost {
tmpl.Execute(w, nil)
return
}
email := r.FormValue("email")
result := "Invalid or unreachable email domain."
if validateEmailDomain(email) {
result = "Email domain is valid and reachable."
}
tmpl.Execute(w, result)
}
func main() {
http.HandleFunc("/", handler)
log.Println("Starting server on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}

Creating and Pushing the Docker Image

The Docker image is created and pushed using the following commands:

docker build -t email-verifier .
docker tag email-verifier [username]/email-verifier
docker push [username]/email-verifier

Live Link and Repository

sorry about the grammar/typo in the website

Get Amal Mullangath’s stories in your inbox

Join Medium for free to get updates from this writer.

Live Link:

GitHub Repository:

  • Docker Image:

Important Notice About Live Link: Please note that the live link associated with this project might be taken down in the future. While this blog post will remain, the real-time demonstration may not be available indefinitely.

Conclusion

We’ve covered the development of an Email Verification System in Go, from code to cloud deployment. Feel free to explore the code, fork the repository, and use the live link while it’s available. We’re eager to hear your feedback and experiences!

Few screenshots

Press enter or click to view image in full size
Azure VM overview
Press enter or click to view image in full size
docker ps -a >> output
Press enter or click to view image in full size
Press enter or click to view image in full size
docker desktop and live site

Thank you

--

--