In this tutorial, we will look at how we can use the AWS Go SDK to perform various operations on AWS SES.

Table of contents

Prerequisites

  • Install AWS Go SDK: Run go get -u github.com/aws/aws-sdk-go/... to install the SDK
  • AWS Credentials: If you haven’t setup AWS credentials before, this resource from AWS is helpful.

How to verify an email on SES?

Before we can send an email using SES, we need to verify the email address using the VerifyEmailIdentity method.

package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ses"
)

func VerifyEmail(sess *session.Session, email string) error {
	sesClient := ses.New(sess)
	_, err := sesClient.VerifyEmailIdentity(&ses.VerifyEmailIdentityInput{
		EmailAddress: aws.String(email),
	})

	return err
}

func main() {
	sess, err := session.NewSessionWithOptions(session.Options{
		Profile: "default",
		Config: aws.Config{
			Region: aws.String("us-west-2"),
		},
	})

	if err != nil {
		fmt.Printf("Failed to initialize new session: %v", err)
		return
	}

	email := "[email protected]"
	err = VerifyEmail(sess, email)
	if err != nil {
		fmt.Printf("Got an error while trying to verify email: %v", err)
		return
	}
}

You should receive an email asking you to verify the email address: /assets/img/boto3-ses/email-verification.png


How to send an email using SES?

We will be using the SendEmail method from the SDK to send an email. The key parameters for this method are:

  • Source: Email address to send the email from. This email address should be verified with SES before it can be used.
  • Destination: The destination for the email.
  • Message: The actual message we want to send. The message can include both plain-text as well as HTML.
package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ses"
)

const CHARSET = "UTF-8"

func SendEmail(sess *session.Session, toAddresses []*string, emailText string, sender string, subject string) error {
	sesClient := ses.New(sess)

	_, err := sesClient.SendEmail(&ses.SendEmailInput{
		Destination: &ses.Destination{
			CcAddresses: []*string{},
			ToAddresses: toAddresses,
		},
		Message: &ses.Message{
			Body: &ses.Body{
				Text: &ses.Content{
					Charset: aws.String(CHARSET),
					Data:    aws.String(emailText),
				},
			},
			Subject: &ses.Content{
				Charset: aws.String(CHARSET),
				Data:    aws.String(subject),
			},
		},
		Source: aws.String(sender),
	})

	return err
}

func SendHTMLEmail(sess *session.Session, toAddresses []*string, htmlText string, sender string, subject string) error {
	sesClient := ses.New(sess)

	_, err := sesClient.SendEmail(&ses.SendEmailInput{
		Destination: &ses.Destination{
			CcAddresses: []*string{},
			ToAddresses: toAddresses,
		},
		Message: &ses.Message{
			Body: &ses.Body{
				Html: &ses.Content{
					Charset: aws.String(CHARSET),
					Data:    aws.String(htmlText),
				},
			},
			Subject: &ses.Content{
				Charset: aws.String(CHARSET),
				Data:    aws.String(subject),
			},
		},
		Source: aws.String(sender),
	})

	return err
}


func main() {
	sess, err := session.NewSessionWithOptions(session.Options{
		Profile: "default",
		Config: aws.Config{
			Region: aws.String("us-west-2"),
		},
	})

	if err != nil {
		fmt.Printf("Failed to initialize new session: %v", err)
		return
	}

	toAddresses := []*string{
		aws.String("[email protected]"),
	}
	emailText := "Amazing SES Tutorial"
	htmlText := "<html>"
            "<head></head>"
            "<h1 style='text-align:center'>This is the heading</h1>"
            "<p>Hello, world</p>"
            "</body>"
            "</html>"
	sender := "[email protected]"
	subject := "Amazing Email Tutorial!"

	err = SendEmail(sess, toAddresses, emailText, sender, subject)
	if err != nil {
		fmt.Printf("Got an error while trying to send email: %v", err)
		return
	}

	fmt.Println("Sent text email successfully")

	err = SendHTMLEmail(sess, toAddresses, htmlText, sender, subject)
	if err != nil {
		fmt.Printf("Got an error while trying to send email: %v", err)
		return
	}

	fmt.Println("Sent html email successfully")

}

/assets/img/boto3-ses/plain-email.png

/assets/img/boto3-ses/email-html.png


How to send an email with attachments using SES?

The Go SDK provides another method to send emails called SendRawEmail which we will use to send attachments to our email.

We will use the Go Mail package to generate these emails before using the AWS SDK to send an email with the attachment.


package main

import (
	"bytes"
	"fmt"

	"gopkg.in/gomail.v2"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ses"
)

const CHARSET = "UTF-8"

func SendEmailAttachment(sess *session.Session, recipient string, sender string, subject string, htmlText string) error {
	msg := gomail.NewMessage()
	msg.SetHeader("From", sender)
	msg.SetHeader("To", recipient)
	msg.SetHeader("Subject", subject)
	msg.SetBody("text/html", htmlText)

    // cat image in the same folder
	msg.Attach("cat.jpeg")

	var emailRaw bytes.Buffer
	msg.WriteTo(&emailRaw)

	message := ses.RawMessage{
		Data: emailRaw.Bytes(),
	}

	sesClient := ses.New(sess)
	_, err := sesClient.SendRawEmail(&ses.SendRawEmailInput{
		RawMessage: &message,
		Destinations: []*string{
			aws.String(recipient),
		},
		Source: aws.String(sender),
	})

	return err
}
}

func main() {
	sess, err := session.NewSessionWithOptions(session.Options{
		Profile: "default",
		Config: aws.Config{
			Region: aws.String("us-west-2"),
		},
	})

	if err != nil {
		fmt.Printf("Failed to initialize new session: %v", err)
		return
	}

	htmlText := "<h1>Amazon SES Test Email (AWS SDK for Go)</h1><p>This email was sent with " +
		"<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the " +
		"<a href='https://aws.amazon.com/sdk-for-go/'>AWS SDK for Go</a>.</p>"
	sender := "[email protected]"
	subject := "Hello, world!"

	err = SendEmailAttachment(sess, sender, sender, subject, htmlText)

	if err != nil {
		fmt.Printf("Got an error while trying to send email: %v", err)
		return
	}

	fmt.Println("Sent email with attachment successfully")
}

/assets/img/boto3-ses/email-attachment.png