DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Automating Email Flow Validation with Go and Open Source Tools

In modern DevOps pipelines, ensuring reliable email delivery and flow validation is crucial for maintaining user engagement and operational efficiency. Manually testing email flows can be tedious and error-prone, especially as systems scale. Leveraging Go, with its simplicity and performance, combined with open source tools, offers an elegant, automated solution.

Understanding the Challenge

Email flow validation involves testing various scenarios: sending emails to ensure delivery, verifying content, checking recipient handling, and confirming bounce/complaint processing. Traditional methods often rely on manual testing or third-party services, which may introduce delays or lack customization.

Solution Overview

By building a Go-based tool that integrates with open source email servers and message queues, we can create a repeatable, automated validation process. Our approach involves setting up a local SMTP server or using open source cloud SMTP relay options, sending test emails via Go, and then analyzing server responses and delivery statuses.

Implementation Details

1. Setting Up an Open Source SMTP Server

For testing, you can use tools like MailHog, which acts as a fake SMTP server, capturing all emails sent for inspection.

docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
Enter fullscreen mode Exit fullscreen mode

MailHog captures email traffic without requiring actual delivery, making it perfect for validation.

2. Sending Emails with Go

Using the net/smtp package, you can craft and send emails programmatically.

package main

import (
    "log"
    "net/smtp"
)

func main() {
    smtpServer := "localhost"
    smtpPort := "1025" // MailHog default port
    auth := smtp.PlainAuth("", "", "", smtpServer)
    from := "test@example.com"
    to := []string{"recipient@example.com"}
    message := []byte("Subject: Test Email\r\n\r\nThis is a test.")

    err := smtp.SendMail(smtpServer+":"+smtpPort, auth, from, to, message)
    if err != nil {
        log.Fatalf("Failed to send email: %v", err)
    }
    log.Println("Email sent successfully")
}
Enter fullscreen mode Exit fullscreen mode

This script sends a test email to MailHog, which captures the message.

3. Automating Validation and Analyzing Results

You can extend the script to query MailHog's API, which provides JSON data about captured emails. This step allows verifying email content, headers, and delivery status.

package main

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

type MailHogMessage struct {
    Items []struct {
        Content struct {
            Subject string `json:"Subject"`
            Body    string `json:"Body"`
        } `json:"Content"`
        Raw struct {
            Data string `json:"Data"`
        } `json:"Raw"`
    } `json:"Items"`
}

func fetchEmails() {
    resp, err := http.Get("http://localhost:8025/api/v2/search")
    if err != nil {
        fmt.Printf("Error fetching emails: %v\n", err)
        return
    }
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    var messages MailHogMessage
    json.Unmarshal(body, &messages)
    for _, item := range messages.Items {
        fmt.Printf("Subject: %s\n", item.Content.Subject)
        fmt.Printf("Body Preview: %s\n", item.Content.Body[:50])
    }
}

func main() {
    fetchEmails()
}
Enter fullscreen mode Exit fullscreen mode

This enables automated checks for delivery confirmation and content validation.

Benefits of This Approach

  • Automation: Fully scriptable and repeatable validation runs.
  • Open Source: No reliance on proprietary services, reducing costs and increasing control.
  • Flexibility: Easily extendable to include bounce processing, content checks, or integration with CI/CD pipelines.
  • Scalability: Suitable for testing systems with high email throughput.

Final Thoughts

By combining Go's network capabilities with open source tools like MailHog, DevOps teams can create robust, automated email validation workflows. This setup not only improves reliability but also accelerates deployment cycles by catching email issues early in the pipeline.

For more complex validation, consider integrating with message queues like RabbitMQ or Kafka, or extend the system to validate delivery receipts from real SMTP servers. Embracing open source tools and versatile programming languages like Go empowers teams to build resilient, scalable email workflows.

References:


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)