123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package mail
- import (
- "fmt"
- "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
- "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/types"
- //go get -u github.com/aws/aws-sdk-go
- "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"
- )
- const (
- // The character encoding for the email.
- AwsCharSet = "UTF-8"
- )
- var (
- AwsErrorCodeMap = map[string]string{
- ses.ErrCodeMessageRejected: ses.ErrCodeMessageRejected,
- ses.ErrCodeMailFromDomainNotVerifiedException: ses.ErrCodeMailFromDomainNotVerifiedException,
- ses.ErrCodeConfigurationSetDoesNotExistException: ses.ErrCodeConfigurationSetDoesNotExistException,
- }
- )
- type AwsSESAdapter struct {
- }
- func (adapter *AwsSESAdapter) Send(mail *Mail, options *SendMailOptions) (err *errs.Error) {
- var (
- sendError error
- result *ses.SendEmailOutput
- sess *session.Session
- )
- sess, sendError = session.NewSession(&aws.Config{
- Region: aws.String("us-east-1")},
- )
- // Create an SES session.
- svc := ses.New(sess)
- destination := mail.Destination
- body := mail.Body
- // Assemble the email.
- input := &ses.SendEmailInput{
- Destination: &ses.Destination{
- CcAddresses: destination.CcAddresses,
- ToAddresses: destination.ToAddresses,
- },
- Source: mail.Sender,
- Message: &ses.Message{
- Subject: &ses.Content{
- Charset: aws.String(AwsCharSet),
- Data: mail.Subject,
- },
- Body: &ses.Body{
- Html: &ses.Content{
- Charset: aws.String(AwsCharSet),
- Data: body.Html,
- },
- Text: &ses.Content{
- Charset: aws.String(AwsCharSet),
- Data: body.Text,
- },
- },
- },
- // Uncomment to use a configuration set
- // ConfigurationSetName: aws.String(ConfigurationSet),
- }
- // Attempt to send the email.
- if result, sendError = svc.SendEmail(input); sendError != nil {
- err = adapter.handlerError(sendError)
- return
- }
- fmt.Println("Email Sent to address: ")
- fmt.Println(result)
- return
- }
- func (adapter *AwsSESAdapter) SendTemplateEmail(mail *Mail, options *SendMailOptions) (err *errs.Error) {
- var (
- sendError error
- jsonData string
- sess *session.Session
- result *ses.SendTemplatedEmailOutput
- )
- sess, sendError = session.NewSession(&aws.Config{
- Region: aws.String("us-east-1")},
- )
- // Create an SES session.
- svc := ses.New(sess)
- if mail.TemplateData == nil {
- mail.TemplateData = &types.Map{}
- }
-
- if jsonData, err = types.JSON(mail.TemplateData); err != nil {
- return
- }
- destination := mail.Destination
- // Assemble the email.
- // input := &ses.SendEmailInput{
- input := &ses.SendTemplatedEmailInput{
- Template: mail.Template,
- TemplateData: aws.String(jsonData),
- Destination: &ses.Destination{
- CcAddresses: destination.CcAddresses,
- ToAddresses: destination.ToAddresses,
- },
- Source: mail.Sender,
- // Uncomment to use a configuration set
- // ConfigurationSetName: aws.String(ConfigurationSet),
- }
- // Attempt to send the email.
- // Display error messages if they occur.
- if result, sendError = svc.SendTemplatedEmail(input); sendError != nil {
- err = adapter.handlerError(sendError)
- return
- }
- fmt.Println("Email Sent to address:")
- fmt.Println(result)
- return
- }
- func (adapter *AwsSESAdapter) handlerError(sendError error) (err *errs.Error) {
- var (
- message string
- reason string
- found bool
- )
- if aerr, ok := sendError.(awserr.Error); ok {
- if reason, found = AwsErrorCodeMap[aerr.Code()]; !found {
- reason = ""
- }
- message = aerr.Error()
- } else {
- message = err.Error()
- }
- err = errs.Internal().Details(&errs.Detail{
- Reason : reason,
- Message: message,
- })
- return
- }
|