How to send emails in Golang
Hello! In this little post I will show you how to send emails in Golang using Sendgrid.
First of all you need to create an account, it is extremely easy to create one, follow these steps:
- Go to https://app.sendgrid.com/ and click on “Start for free”:
2. Then you will see something like this:
Enter an email and a password, you are almost done (make sure to confirm your email address…)
3. Add a few more details and click on “Get Started!”.
4. Make sure you confirm your account:
5. Once you confirm your account, you need to create a Sender Identity, Click on the “Create a Single Sender” button:
6. Fill some details in the next window:
The Sender Identity will be basically the from field that will be used in the Golang code. Once you fill up those details, you will get this window:
At this point, the Sender Identity is not verified yet.
7. You need to log in to the email account you used in the previous window and verify it:
We are almost done! The last configuration step is just to create a API Key.
8. Under the Settings section, clic on API Keys:
9. Write a name for your key and click on “Create & View”:
10. You will be able to see your API Key value in the next window, save it so we can use it in the code:
We are done with the Sendgrid configuration for simple tests, now we can jump to the code! 😀
11. To be able to send emails from Go, you need to install the following two packages:
- github.com/sendgrid/sendgrid-go
- github.com/sendgrid/sendgrid-go/helpers/mail
To install them, run the following two commands:
go get -v -u "github.com/sendgrid/sendgrid-go"go get -v -u "github.com/sendgrid/sendgrid-go/helpers/mail"
You will see something like this:
0 🐧 [16:46]leo@lein ~ $ go get -v -u "github.com/sendgrid/sendgrid-go"
github.com/sendgrid/sendgrid-go (download)
github.com/sendgrid/rest (download)
github.com/sendgrid/sendgrid-go/helpers/mail
github.com/sendgrid/sendgrid-go
0 🐧 [16:46]leo@lein ~ $ go get -v -u "github.com/sendgrid/sendgrid-go/helpers/mail"
github.com/sendgrid/sendgrid-go (download)
0 🐧 [16:47]leo@lein ~ $
12. I suggest to save your API Key in an environment variable, this way you reference the environment variable name only and not the value. You protect yourself from checkin in the code in a code repository. To save your API Key, just add the following line to your .bashrc file.
export EMAIL_API_KEY="..."
13. Make sure you re-read your Bash configuration, for that run the following command:
source ~/.bashrc
14. This is the Golang code:
package mainimport (
"fmt"
"os""github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)func notifyEmail(msg string) error {
from := mail.NewEmail("Leo", "from...@email.com")
subject := fmt.Sprintf("REMINDER -> %s", msg)
to := mail.NewEmail("to...@email.com", "to...@email.com")message := mail.NewSingleEmail(from, subject, to, msg, msg)
client := sendgrid.NewSendClient(os.Getenv("EMAIL_API_KEY"))
_, err := client.Send(message)return err
}func main() {
err := notifyEmail("Hello!!!!")
if err != nil {
panic(err)
}
}
Pay special attention to the from and client variables.
You can see in the code that I am referencing the environment variable I just created. Of course, the code can be improved, I am not focusing on that in this post.
Build the code with go build command and run the program.
The email may go to the SPAM folder, make sure you check that folder.
As you can see, it is very easy to send emails, I use this all the time for side/pet projects.
What other ways do you know for sending emails? Please let me know in the comments.
Thank you!