How to create a logic for sending mails with Go and AWS SES

Tomoharu Tsutsumi
1 min readApr 26, 2023

That was a little tough for me because I didn’t have any knowledge of them. I’ve managed to get it so I’ll summarize them.

The code

package mailer

import (
"context"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
)

func Send(message string) bool {
from := os.Getenv("MAIL_FROM")
recipients := []string{os.Getenv("MAIL_RECIPIENTS")}
region := "XXXX"
subject := "You've gotten an message from a user!"

ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
if err != nil {
fmt.Println(err)
}
client := sesv2.NewFromConfig(cfg)
input := &sesv2.SendEmailInput{
FromEmailAddress: &from,
Destination: &types.Destination{
ToAddresses: recipients,
},
Content: &types.EmailContent{
Simple: &types.Message{
Body: &types.Body{
Text: &types.Content{
Data: &message,
},
},
Subject: &types.Content{
Data: &subject,
},
},
},
}
res, err := client.SendEmail(ctx, input)
if err != nil {
fmt.Println(err)
return false
}
fmt.Println(res.MessageId)
return true
}

and

AWS_SECRET_KEY=XXXXXXXXXXX
AWS_ACCESS_KEY_ID=XXXXXXXXXXXX

That’s all.

If you are using Docker, you have to add like below in Dockerfile.

RUN go get github.com/aws/aws-sdk-go-v2/config
RUN go get github.com/aws/aws-sdk-go-v2/service/sesv2
RUN go get github.com/aws/aws-sdk-go-v2/service/sesv2/types

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)