In this article, you will learn How to send an email with custom templates using AWS’s Simple Mail Services(SES).
After implementing it in one of our company’s projects, I thought it could be useful for others too, So I decided to write on this topic.
So, let’s start…
We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!
We are going to create an HTML template that will bind dynamic variables. let’s create an Html file with the name email-template.html
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body>
<p> Hello {{.FirstName}} {{.LastName}} </p>
<p> Welcome to AWS Simple Mail Services with static Templates World. </p>
<p> Best Regards from, </p>
<p> Your Friend </p>
</body>
</html>
In the above code snippet, We will bind the user’s first name in {{.FirstName}}
and last name in {{.LastName}}
.
import (
"html/template"
"io/ioutil"
)
func getHTMLTemplate() string {
var templateBuffer bytes.Buffer
type EmailData struct {
FirstName string
LastName string
}
// You can bind custom data here as per requirements.
data := EmailData{
FirstName: "John",
LastName: "Doe",
}
htmlData, err := ioutil.ReadFile("email-template.html")
htmlTemplate := template.Must(template.New("email.html").Parse(string(htmlData)))
err = htmlTemplate.ExecuteTemplate(&templateBuffer, "email.html", data)
if err != nil {
log.Fatal(err)
return ""
}
return templateBuffer.String()
}
Here we created templateBuffer that will be used by SES email templates.
EmailData: Contains data that will bind to the HTML template.
htmlData: Read HTML file using ioutil’s ReadFile method and store bytes in htmlData.
htmlTemplate : Parse htmlData to new “email.html” template.
templateBuffer: ExecuteTemplate applies the HTML template with the specified data object and writes the output to templateBuffer.
We will import aws-sdk-go’s aws and ses packages for sending emails.
import (
"bytes"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
)
func GenerateSESTemplate() (template *ses.SendEmailInput) {
html := getHTMLTemplate()
title := "Sample Email"
template = &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
aws.String(&receiver),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String("utf-8"),
Data: aws.String(html),
},
},
Subject: &ses.Content{
Charset: aws.String("utf-8"),
Data: aws.String(title),
},
},
Source: aws.String(&sender),
}
return
}
html: get the template from getHTMLTemplate()
template: It contains a request for a formatted email from SendEmailInput. We can also add a Text field in the Message Body.
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ses"
)
func SendEmail() {
emailTemplate := GenerateSESTemplate()
sess, err := session.NewSession(&aws.Config{
Region: aws.String(®ion),
Credentials: credentials.NewStaticCredentials(&accessKeyId, &secretAccessKey, ""),
})
if err != nil {
log.Fatal(err)
}
service := ses.New(sess)
// Attempt to send the email.
_, err = service.SendEmail(emailTemplate)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
log.Fatal(aerr.Error())
} else {
log.Fatal(err)
}
}
}
SendEmail gets email data from GenerateSESTemplate() and sends an email with that data using SES’s SendEmail() method.
That’s it. Now you can send emails by changing your sender and receiver in the email template.
The email will look like below,
We’re Grateful to have you with us on this journey!
Suggestions and feedback are more than welcome!
Please reach us at Canopas Twitter handle @canopas_eng with your content or feedback. Your input enriches our content and fuels our motivation to create more valuable and informative articles for you.
Get started today
Let's build the next
big thing!
Let's improve your business's digital strategy and implement robust mobile apps to achieve your business objectives. Schedule Your Free Consultation Now.
Get Free ConsultationGet started today
Let's build the next big thing!
Let's improve your business's digital strategy and implement robust mobile apps to achieve your business objectives. Schedule Your Free Consultation Now.
Get Free Consultation