Sending HTML Emails Using Golang

In this tutorial we’ll use Gmail’s SMTP server to send HTML emails.

Before proceeding make sure the Gmail account you’ll use to send email allows less secure apps.

CREATE A NEW PROJECT

package main

import "net/smtp"
import "log"

func send(body string, to string) {
    
}

func main() {
    send("Write your message here.", "recipient@gmail.com")    
}

ADD YOUR CREDENTIALS

func send(body string, to string) {
    from := "youremail@gmail.com"
    password := "yourpassword"
}

COMPOSE THE MESSAGE

func send(body string, to string) {
    from := "youremail@gmail.com"
    password := "yourpassword"

    msg := "From: " + from + "\r\n" +
           "To: " + to + "\r\n" + 
           "Subject: Your messages subject" + "\r\n\r\n" +
           body + "\r\n"
}

SEND THE EMAIL

func send(body string, to string) {
    from := "youremail@gmail.com"
    password := "yourpassword"

    msg := "From: " + from + "\r\n" +
           "To: " + to + "\r\n" + 
           "Subject: Your messages subject" + "\r\n\r\n" +
           body + "\r\n"

    err := smtp.SendMail("smtp.gmail.com:587", smtp.PlainAuth("", from, password, "smtp.gmail.com"), from, []string{to}, []byte(msg))
    if err != nil {
        log.Printf("Error: %s", err)
        return
    }

    log.Print("message sent")
}

Golang email sent

In order to send HTML add two new values to the message and change the body to HTML

func send(body string, to string) {
    from := "youremail@gmail.com"
    password := "yourpassword"
    
    // Add MIME-Version and Content-type
    msg := "From: " + from + "\r\n" +
           "To: " + to + "\r\n" + 
           "MIME-Version: 1.0" +  "\r\n" +
           "Content-type: text/html" + "\r\n" +
           "Subject: Your messages subject" + "\r\n\r\n" +
           body + "\r\n"

    err := smtp.SendMail("smtp.gmail.com:587", smtp.PlainAuth("", from, password, "smtp.gmail.com"), from, []string{to}, []byte(msg))
    if err != nil {
        log.Printf("Error: %s", err)
        return
    }

    log.Print("message sent")
}

func main() {
    // Send html in the body
    send("<h1>Write your message here.</h1>", "recipient@gmail.com")    
}

Golang html email sent

Done but for those that would like to implement templates keep reading.

TEMPLATES

Create a template named template.html


{{ template "email" }}
<h1>Hello {{ .Name }}</h1>
<p>{{ .Body  }}</p>
{{ end }}

Create a new struct to contain the template variables

import "html/template"
import "bytes"

type email struct {
    Name string
    Body string
}

Create a new template and send it in the message

func send(body string, to string) {
    // Create the variables for the template
    myEmail := email{"John", body}

    // Create a template using template.html
    tmpl, err := template.New("email").ParseFiles("./template.html")
    if err != nil {
        log.Printf("Error: %s", err)
        return
    }

    // Stores the parsed template
    var buff bytes.buffer 

    // Send the parsed template to buff 
    err = tmpl.Execute(&buff, myEmail)
    if err != nil {
        log.Printf("Error: %s", err)    
    }

    from := "youremail@gmail.com"
    password := "yourpassword"

    // replace body with buff.String()
    msg := "From: " + from + "\r\n" +
           "To: " + to + "\r\n" + 
           "MIME-Version: 1.0" + "\r\n" +
           "Content-type: text/html" + "\r\n" +
           "Subject: Your messages subject" + "\r\n\r\n" +
           buff.String() + "\r\n"

    err := smtp.SendMail("smtp.gmail.com:587", smtp.PlainAuth("", from, password, "smtp.gmail.com"), from, []string{to}, []byte(msg))
    if err != nil {
        log.Printf("Error: %s", err)
        return
    }

    log.Print("message sent")
}

func main() {
    send("Write your message here.", "recipient@gmail.com")    
}