I wanted to send an email from my TraceLog package when a critical exception occurred. Fortunately Go’s standard library has a package called smpt which can be found inside the net package. When you look at the documentation you are left wanting.

I spent 20 minutes researching how to use this package. After fighting through the parameters and bugs, I came up with this sample code:

package main

import (
    "bytes"
    "fmt"
    "net/smtp"
    "runtime"
    "strings"
    "text/template"
)

func main() {
    SendEmail(
        "smtp.1and1.com",
        587,
        "username@domain.com",
        "password",
        []string{"me@domain.com"},
        "testing subject",
        "<html><body>Exception 1</body></html>Exception 1")
    }

func catchPanic(err *error, functionName string) {
    if r := recover(); r != nil {
        fmt.Printf("%s : PANIC Defered : %v\n", functionName, r)

        // Capture the stack trace
        buf := make([]byte, 10000)
        runtime.Stack(buf, false)

        fmt.Printf("%s : Stack Trace : %s", functionName, string(buf))

        if err != nil {
            *err = fmt.Errorf("%v", r)
        }
    } else if err != nil && *err != nil {
        fmt.Printf("%s : ERROR : %v\n", functionName, *err)

        // Capture the stack trace
        buf := make([]byte, 10000)
        runtime.Stack(buf, false)

        fmt.Printf("%s : Stack Trace : %s", functionName, string(buf))
    }
}

func SendEmail(host string, port int, userName string, password string, to []string, subject string, message string) (err error) {
    defer catchPanic(&err, "SendEmail")

    parameters := struct {
        From string
        To string
        Subject string
        Message string
    }{
        userName,
        strings.Join([]string(to), ","),
        subject,
        message,
    }

    buffer := new(bytes.Buffer)

    template := template.Must(template.New("emailTemplate").Parse(emailScript()))
    template.Execute(buffer, &parameters)

    auth := smtp.PlainAuth("", userName, password, host)

    err = smtp.SendMail(
        fmt.Sprintf("%s:%d", host, port),
        auth,
        userName,
        to,
        buffer.Bytes())

    return err
}

func emailScript() (script string) {
    return `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"

{{.Message}}`
}

The auth variable does not have to be recreated on every call. That can be created once and reused. I added my catchPanic function so you can see any exceptions that are returned while testing the code.

If you look at the raw source of the email you will see how the message parameter works:

Return-Path:
Delivery-Date: Wed, 12 Jun 2013 20:34:59 -0400
Received: from mout.perfora.net (mout.perfora.net [X.X.X.X])
    by mx.perfora.net (node=mxus0) with ESMTP (Nemesis)
    id 0MZTCn-1V3EP13 for me@domain.com; Wed, 12 Jun 2013 20:34:58 -0400
Received: from localhost (c-50-143-31-151.hsd1.fl.comcast.net [X.X.X.X])
    by mrelay.perfora.net (node=mrus4) with ESMTP (Nemesis)
    id 0Mhi4R-1V0RuN48ot-00MTc6; Wed, 12 Jun 2013 20:34:58 -0400
From: username@domain.com
To: me@domain.com
Subject: testing subject
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"
Message-Id: <0mhi4r- data-blogger-escaped-mrelay.perfora.net="">
Date: Wed, 12 Jun 2013 20:34:56 -0400
Envelope-To: me@domain.com


<html><body>Exception 1</body></html>


As always I hope this sample program saves you time and aggravation.

Trusted by top technology companies

We've built our reputation as educators and bring that mentality to every project. When you partner with us, your team will learn best practices and grow along the way.

30,000+

Engineers Trained

1,000+

Companies Worldwide

12+

Years in Business