Open Source Go Library for Testing Email Messages
Free Email Testing Tool for developer, developed in GO. It allows to Generate Email Message with Attachments, Encrypt/Decrypt Messages with PGP/MIME.
What is MailHog Go Library?
MailHog is an Open Source email testing tool for developers. You can configure your applications to use MailHog for SMTP delivery, you can retrieve email messages with the JSON API or view them in the web UI. You can also end messages to real SMTP servers.
MailHog implement ESMTP server implementation, supports SMTP AUTH and PIPELINING, provides a Web interface to view text or HTML emails, displays real-time email updates, and releases emails to real SMTP servers. Furthermore, the API supports multipart MIME & allows downloading individual MEME parts. MailHog uses In-memory message storage and uses MongoDB and file-based storage for message persistence.
Getting Started with MailHog
MailHog is built with GO that runs without installation on multiple platforms
Run MailHog on MacOS
brew update && brew install mailhog
You can start running MailHog in MacOs by running mailhog in the command line.Configure MailHog for Outgoing SMTP
In order to configure outgoing SMTP you need to create JSON file with the following structure and set MH_OUTGOING_SMTP or -outgoing-smtp.
{
"server name": {
"name": "server name",
"host": "...",
"port": "587",
"email": "...",
"username": "...",
"password": "...",
"mechanism": "PLAIN"
}
}
In the JSON file, only name, host and port are required to send SMPT email.
Sending an Email Message using Go Library
Sending emails from within Go applications is made simple for software developers by the open-source MailHog module. The library's ability to simulate an SMTP server is its primary feature. All outbound emails are intercepted by this feature, keeping them from getting to the intended recipients. It gives developers a secure setting where they can test email features without worrying about annoying actual users. The SMTP server of MailHog, which is executing on localhost at port 1025, is used to send an email in the example below using the smtp.SendMail function.
How to Send an Email Message via MailHog's SMTP Server?
package main
import (
"log"
"net/smtp"
)
func main() {
// Set up the SMTP server configuration
smtpServer := "localhost:1025"
// Sender data
from := "sender@example.com"
password := "" // MailHog does not require a password
// Receiver email address
to := []string{"recipient@example.com"}
// Message
message := []byte("Subject: Test Email\n" +
"This is a test email sent from a Go application.")
// Authentication
auth := smtp.PlainAuth("", from, password, "localhost")
// Sending email
err := smtp.SendMail(smtpServer, auth, from, to, message)
if err != nil {
log.Fatal(err)
}
log.Println("Email sent successfully")
}