adapter_aws_ses.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package mail
  2. import (
  3. "fmt"
  4. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
  5. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/types"
  6. //go get -u github.com/aws/aws-sdk-go
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/awserr"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/ses"
  11. )
  12. const (
  13. // The character encoding for the email.
  14. AwsCharSet = "UTF-8"
  15. )
  16. var (
  17. AwsErrorCodeMap = map[string]string{
  18. ses.ErrCodeMessageRejected: ses.ErrCodeMessageRejected,
  19. ses.ErrCodeMailFromDomainNotVerifiedException: ses.ErrCodeMailFromDomainNotVerifiedException,
  20. ses.ErrCodeConfigurationSetDoesNotExistException: ses.ErrCodeConfigurationSetDoesNotExistException,
  21. }
  22. )
  23. type AwsSESAdapter struct {
  24. }
  25. func (adapter *AwsSESAdapter) Send(mail *Mail, options *SendMailOptions) (err *errs.Error) {
  26. var (
  27. sendError error
  28. result *ses.SendEmailOutput
  29. sess *session.Session
  30. )
  31. sess, sendError = session.NewSession(&aws.Config{
  32. Region: aws.String("us-east-1")},
  33. )
  34. // Create an SES session.
  35. svc := ses.New(sess)
  36. destination := mail.Destination
  37. body := mail.Body
  38. // Assemble the email.
  39. input := &ses.SendEmailInput{
  40. Destination: &ses.Destination{
  41. CcAddresses: destination.CcAddresses,
  42. ToAddresses: destination.ToAddresses,
  43. },
  44. Source: mail.Sender,
  45. Message: &ses.Message{
  46. Subject: &ses.Content{
  47. Charset: aws.String(AwsCharSet),
  48. Data: mail.Subject,
  49. },
  50. Body: &ses.Body{
  51. Html: &ses.Content{
  52. Charset: aws.String(AwsCharSet),
  53. Data: body.Html,
  54. },
  55. Text: &ses.Content{
  56. Charset: aws.String(AwsCharSet),
  57. Data: body.Text,
  58. },
  59. },
  60. },
  61. // Uncomment to use a configuration set
  62. // ConfigurationSetName: aws.String(ConfigurationSet),
  63. }
  64. // Attempt to send the email.
  65. if result, sendError = svc.SendEmail(input); sendError != nil {
  66. err = adapter.handlerError(sendError)
  67. return
  68. }
  69. fmt.Println("Email Sent to address: ")
  70. fmt.Println(result)
  71. return
  72. }
  73. func (adapter *AwsSESAdapter) SendTemplateEmail(mail *Mail, options *SendMailOptions) (err *errs.Error) {
  74. var (
  75. sendError error
  76. jsonData string
  77. sess *session.Session
  78. result *ses.SendTemplatedEmailOutput
  79. )
  80. sess, sendError = session.NewSession(&aws.Config{
  81. Region: aws.String("us-east-1")},
  82. )
  83. // Create an SES session.
  84. svc := ses.New(sess)
  85. if mail.TemplateData == nil {
  86. mail.TemplateData = &types.Map{}
  87. }
  88. if jsonData, err = types.JSON(mail.TemplateData); err != nil {
  89. return
  90. }
  91. destination := mail.Destination
  92. // Assemble the email.
  93. // input := &ses.SendEmailInput{
  94. input := &ses.SendTemplatedEmailInput{
  95. Template: mail.Template,
  96. TemplateData: aws.String(jsonData),
  97. Destination: &ses.Destination{
  98. CcAddresses: destination.CcAddresses,
  99. ToAddresses: destination.ToAddresses,
  100. },
  101. Source: mail.Sender,
  102. // Uncomment to use a configuration set
  103. // ConfigurationSetName: aws.String(ConfigurationSet),
  104. }
  105. // Attempt to send the email.
  106. // Display error messages if they occur.
  107. if result, sendError = svc.SendTemplatedEmail(input); sendError != nil {
  108. err = adapter.handlerError(sendError)
  109. return
  110. }
  111. fmt.Println("Email Sent to address:")
  112. fmt.Println(result)
  113. return
  114. }
  115. func (adapter *AwsSESAdapter) handlerError(sendError error) (err *errs.Error) {
  116. var (
  117. message string
  118. reason string
  119. found bool
  120. )
  121. if aerr, ok := sendError.(awserr.Error); ok {
  122. if reason, found = AwsErrorCodeMap[aerr.Code()]; !found {
  123. reason = ""
  124. }
  125. message = aerr.Error()
  126. } else {
  127. message = err.Error()
  128. }
  129. err = errs.Internal().Details(&errs.Detail{
  130. Reason : reason,
  131. Message: message,
  132. })
  133. return
  134. }