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:
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.