Sending Email With Go

It is easier than you think.

Fernando Souza
Vacatronics

--

Photo by Liam Truong on Unsplash

Recently, I had to program a small routine that would send email messages to a group of contacts as soon as a condition occurred.

Using Go you can easily achieve this.

Note: this tutorial was done using Go version 1.15.8.

Using package smtp

If you don’t want to install any third party package, Go offers you the net/stmp package out of the box.

All you need are two functions:

  • PlainAuth: it provides the plain authentication mechanism using username and password.
  • SendMail: the function that actually sends the mail. It connects to the mail server using the authentication created before (automatically handles TLS, if available) and sends the mail.

Note 1: change the values above to match your data.

Note 2: do not use sensitive data (like your password) directly into the code. Instead, use env variables for configuration. You can check a package like Viper to…

--

--

Fernando Souza
Vacatronics

Enthusiast of programming, electronics, technology and beer, not necessarily in that order. BuyMeACoffee: buymeacoffee.com/ustropo