Skip to content
Mailer package for Golang supports sending SMTP email using template
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Update example Aug 11, 2019
.gitignore Init commit Aug 9, 2019 Update readme Aug 18, 2019
client.go Use encryption as string Aug 13, 2019
message.go Refactoring code Aug 11, 2019
template.go Fix type alias Aug 9, 2019

Go Mailer

Mailer package for Golang supports sending SMTP email using html/template.


Run the following command to install this package:

$ go get -u


The following example will help you know how to use this package:

Note: Go to example directory to see more examples.

package main

import (

func main() {
    // 1. Create the email client
    client := gomailer.NewClient(
        "",     // Host
        465,                    // Port
        "<your-username>",      // Username
        "<your-password>",      // Password
        gomailer.EncryptionTls, // Encryption
    // 2. Create the template
    // The first argument is the template path
    // The second argument is the config for your template. 
    // Such as: LayoutFiles, LayoutDirectory, LayoutExtension (available when using LayoutDirectory).
    // These layout files will be included to your template.
    // @see
    template := gomailer.NewTemplate("template/welcome.html", &gomailer.TemplateConfig{
        LayoutDirectory: "template/layouts",
    // 3. Create the template data
    // You can use Map or Struct here. It is flexible.
    templateData := map[string]interface{}{
        "User": map[string]string{
            "FirstName": "Go",
            "LastName":  "Lang",
    // 4. Create the message using the template
    message, err := gomailer.NewTemplateMessage(&gomailer.Message{
        From:    gomailer.NewAddress("", "Example"),
        To:      []*gomailer.Address{{Address: ""}, {Address: ""}},
        Subject: "Welcome to GoMailer",
    }, template, templateData)
    if err != nil {
        fmt.Print("Err could not make template message: ", err)
    // 5. Try to send the message
    if err := client.Send(message); err != nil {
        fmt.Print("Err could not send mail: ", err)
    } else {
You can’t perform that action at this time.