Sending Emails with Go Using a Custom Template

Michael Habib
4 min readSep 28, 2017

--

A few weeks ago I started working on a portfolio website. Although I could have done the entire website using only React, I decided it would be good practice to also create an API server in Go that can handle certain tasks (e.g. sending emails). One of my pages is a contact me page that currently looks like this:

Contact Page

I wanted to send myself an email using a gmail account I made specifically for this contact form. I did not choose Go for any particular reason except for the fact that I have already sent emails with Javascript in the past and figured why not try it in Go and I think Go is awesome.

Step 1: Creating the Necessary Files

You only really need two files, an html file and a go file. They can be in the same directory or not. Just make sure you reference your html file correctly.

Note: You don’t even need an html file, but then you would have to write the html inside a string in the go file. I am also assuming you have your Go environment set up properly. If not check this out: Writing Go Code

Step 2: Creating the Template

The template is very simple html but you can customize it to however you like. Ignore the {{.Name}} and the others, we will get back to them later.

Step 3: Go!

A. Adding Necessary Packages

It will make sense why we need these as you read on

B. Logging into Gmail

Line 14–16: I grabbed my username and password for one of my gmail accounts from environment variables which I had set earlier. Use the PlaneAuth method of the smtp package to log in with your username, password, and host name of the email server.

C. Parsing the Template and Setting Up the Email

Line 20–26: First create a template by parsing our html, and store it in a variable t( we will use it later ). Then declare a bytes.Buffer that will hold the email subject, headers, and our email body(custom html). I found that using a bytes.Buffer type gave me more flexibility than other types used in other solutions online. Sprintf from the fmt package is a handy method that returns a formatted string. You can use it to return a string that contains the subject and headers of the email. That string is then turned to a byte array and written to the bytes buffer using the Write method. So far the bytes buffer contains the subject and headers. We will add the rest next. Note: You have to add two new lines after the headers just like HTTP.

D. Filling our Template with Data and Sending the Email

Line 28–37: We then need to execute the template, the first argument has to implement the io.Writer interface. Since bytes.Buffer types implements the io.Writer interface we can use it by passing in a pointer to our body variable. Why we use a pointer to body instead of the value has to do with how Go implements interfaces, you can read more about it in the docs. The second argument takes in the data we want to pass into our temlplate ( our html ). Remember the {{.Name}} in our html? That is how we reference the data we executed the template with. The dot is our struct and the Name refers to that property. Don’t get confused about the struct syntax, I am just declaring the structure and initializing it in one statement. Using variables in html is just one of the things you can do with templates in go.

Lastly you use the SendMail method that takes the host and port, the Auth type we created earlier, a string for the ‘from’ field , and an array of strings containing email addresses you want to send the email to. The string for the ‘from’ field as far as I could tell does nothing. Maybe it does for other email servers?

If you want to read more about the smtp package and this method here is the link to the docs. You can also read about the text/template package here. I know we used the html/template package but in the godocs the implementation details of templates is in the docs of text/template.

P.S. This was my first blog post! I would love some feedback and other ideas on what to write about next. Also, follow me on twitter

--

--