Sending Email using Amazon SES in Go

Mayur Wadekar
2 min readJul 16, 2021

--

Email / Electronic mail(email or e-mail) is a way of communication between people over electronic devices. Email might contains text data, files, images, etc. Amazon services provide a secure way to transfer your mail over a network. Amazon SES or Amazon Simple Email Service allows developers to send an email within any application.

Image from Unsplash by Brett Jordan

aws-sdk-go is the official AWS SDK for the Go programming language. To install SDK, you have to get it as

go get github.com/aws/aws-sdk-go

Before sending any email / running given code, you must have to register for the Amazon SES service.

When using SDK it is recommended that you must have to set an Environment Credentials. For SDK API references there are rich documentation is available on the official site for many other services like S3 bucket file upload etc.

In a local development environment, we have to set some basic variables which generally used by SDK internally. the region, AWS access key ID, secret access key, and an access token. Those variables are required with your roles and credentials which reside in ~/.aws/credentials file.

Following go code sends a formatted email to the receiver(s).

NOTE: Following code contains some variables which you might get those from environment variables.

Plain Text Email over Amazon SES

Before you start any coding exercise you need to create a session at the very first

sess, err := session.NewSession(&aws.Config{  Region: aws.String("us-east-1")}, )

All the receivers(to, cc, bcc, etc.) are reference types so we need to provide a pointer to the destination struct(possibly cc, bcc parameters might not be required in some cases).

For passing message body, you might parse your HTML code as a raw string and pass it to message. It will automatically convert your raw HTML to a formatted web message for an email.

For sending raw messages with an attachment you can refer following code.

Raw Email over Amazon SES

Before sending any message over SES we need to take care of the following things:

  1. The message must be sent from a verified email address or domain
  2. If your account is still in the Amazon SES sandbox, you may only send to verified addresses or domains, or to email addresses associated with the Amazon SES Mailbox Simulator
  3. The maximum message size is 10 MB including both body and attachments
  4. The message must include at least one recipient email address any of TO, CC, or BCC
  5. The message may not include more than 50 recipients

I have used the following SDK/Lib versions

github.com/aws/aws-sdk-go v1.35.30gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df

Thanks for your patience reading :).

--

--