Created
January 29, 2024 17:47
-
-
Save warlock/a23a155cc9e13b30867933f06b2b9930 to your computer and use it in GitHub Desktop.
AWS Mailer Go Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mailer | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"os" | |
"text/template" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awserr" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/ses" | |
"gopkg.in/yaml.v3" | |
) | |
const ( | |
CharSet = "UTF-8" | |
) | |
type Mailer struct { | |
client *ses.SES | |
config ConfMail | |
} | |
func (m *Mailer) SendEmail(email string, pin string) { | |
htmlBody := templateParse(m.config.HtmlBody, email, pin) | |
textBody := templateParse(m.config.TextBody, email, pin) | |
input := &ses.SendEmailInput{ | |
Destination: &ses.Destination{ | |
CcAddresses: []*string{}, | |
ToAddresses: []*string{ | |
aws.String(email), | |
}, | |
}, | |
Message: &ses.Message{ | |
Body: &ses.Body{ | |
Html: &ses.Content{ | |
Charset: aws.String(CharSet), | |
Data: aws.String(htmlBody), | |
}, | |
Text: &ses.Content{ | |
Charset: aws.String(CharSet), | |
Data: aws.String(textBody), | |
}, | |
}, | |
Subject: &ses.Content{ | |
Charset: aws.String(CharSet), | |
Data: aws.String(m.config.Subject), | |
}, | |
}, | |
Source: aws.String(m.config.Sender), | |
} | |
result, err := m.client.SendEmail(input) | |
if err != nil { | |
if aerr, ok := err.(awserr.Error); ok { | |
switch aerr.Code() { | |
case ses.ErrCodeMessageRejected: | |
fmt.Println(ses.ErrCodeMessageRejected, aerr.Error()) | |
case ses.ErrCodeMailFromDomainNotVerifiedException: | |
fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error()) | |
case ses.ErrCodeConfigurationSetDoesNotExistException: | |
fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error()) | |
default: | |
fmt.Println(aerr.Error()) | |
} | |
} else { | |
fmt.Println(err.Error()) | |
} | |
return | |
} | |
fmt.Println("Email Sent to address: " + email) | |
fmt.Println(result) | |
} | |
func loadConfMail() ConfMail { | |
data, err := os.ReadFile("mailconf.yaml") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
conf := ConfMail{} | |
err = yaml.Unmarshal(data, &conf) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
return conf | |
} | |
func templateParse(body string, email string, pin string) string { | |
entry := &Entry{ | |
Email: email, | |
Pin: pin, | |
} | |
tmpl, err := template.New("t1").Parse(body) | |
if err != nil { | |
panic(err) | |
} | |
var buf bytes.Buffer | |
err = tmpl.Execute(&buf, &entry) | |
if err != nil { | |
panic(err) | |
} | |
return buf.String() | |
} | |
func NewMailer() *Mailer { | |
sess, err := session.NewSession(&aws.Config{ | |
Region: aws.String("eu-west-1")}, | |
) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
svc := ses.New(sess) | |
config := loadConfMail() | |
return &Mailer{ | |
client: svc, | |
config: config, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment