Send Email with Go


December 17, 2013
Written by
SendGrid Team
Contributor
Opinions expressed by Twilio contributors are their own

Send Email with Go

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.

gopher

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:

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!

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.

Recommended For You



Most Popular


Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.