How to Send Email with Go

A while ago, I found myself interested in learning Go. My regular approach to learn something, is building something. For Go, I decided I would learn by implementing the SendGrid-Go library. Initially I searched for a SMTP client that would allow everything SendGrid’s SMTP API supports, but I was not very lucky. Therefore I decided to implement my own (smtpmail). After that I decided to build the SendGrid-Go library on top of my first library and extend it to it would support the Web API too.

So, on with the example. It’s simple to use. First you will need to download and install the library (and its dependencies) by running the following command:

$ go get github.com/sendgrid/sendgrid-go

From the fragment below, you can observe that in order to initialize the client, you must pass your SendGrid credentials. Afterwards, just create a message object and set some properties like destination and sender. Then use the client to send the message. Magic. If everything went according to plan, you won’t receive an error!

package main import ( "fmt" "github.com/sendgrid/sendgrid-go" ) func main() { sg := sendgrid.NewSendGridClient("sendgrid_user", "sendgrid_key") message := sendgrid.NewMail() message.AddTo("yamil@sendgrid.com") message.AddSubject("My first email!") message.AddText("Sending Email from Go using SendGrid") message.AddFrom("yamil.asusta@sendgrid.com") if r := sg.Send(message); r == nil { fmt.Println("Email sent!") } else { fmt.Println(r) } }

There are three ways to send emails using this library. The SendAPI will use the Web API meanwhile SendSMTP will use SMTP as its transport. Additionally, the Send method will try to use SendAPI, if it fails, it will fallback to SendSMTP.

There is a more in-depth documentation found the GitHub repository.

Feel free to give me a shout if something blew up or just to say hi! I’m also going to Gophercon 2014 so if you are going, I would love to meet up.