| 1 | package mail |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/aws/aws-sdk-go-v2/aws" |
| 7 | awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 8 | "github.com/aws/aws-sdk-go-v2/service/sesv2" |
| 9 | sesv2types "github.com/aws/aws-sdk-go-v2/service/sesv2/types" |
| 10 | ) |
| 11 | |
| 12 | // EmailSender is the one SESv2 call the console makes to send as itself |
| 13 | // (its invite fallback), narrowed so tests use a fake. Apps never use it: |
| 14 | // they send by SMTP with their own credential. |
| 15 | type EmailSender interface { |
| 16 | SendEmail(context.Context, *sesv2.SendEmailInput, ...func(*sesv2.Options)) (*sesv2.SendEmailOutput, error) |
| 17 | } |
| 18 | |
| 19 | // NewSESEmailSender builds a sender on the default credential chain, which |
| 20 | // on a box is the host role. |
| 21 | func NewSESEmailSender(ctx context.Context, region string) (EmailSender, error) { |
| 22 | cfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region)) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | return sesv2.NewFromConfig(cfg), nil |
| 27 | } |
| 28 | |
| 29 | // SimpleEmailInput is a plain-text message attributed to tenant and, when |
| 30 | // set, counted through configSet. |
| 31 | func SimpleEmailInput(from, to, subject, body, tenant, configSet string) *sesv2.SendEmailInput { |
| 32 | in := &sesv2.SendEmailInput{ |
| 33 | FromEmailAddress: aws.String(from), |
| 34 | Destination: &sesv2types.Destination{ToAddresses: []string{to}}, |
| 35 | Content: &sesv2types.EmailContent{Simple: &sesv2types.Message{ |
| 36 | Subject: &sesv2types.Content{Data: aws.String(subject), Charset: aws.String("UTF-8")}, |
| 37 | Body: &sesv2types.Body{Text: &sesv2types.Content{Data: aws.String(body), Charset: aws.String("UTF-8")}}, |
| 38 | }}, |
| 39 | TenantName: aws.String(tenant), |
| 40 | } |
| 41 | if configSet != "" { |
| 42 | in.ConfigurationSetName = aws.String(configSet) |
| 43 | } |
| 44 | return in |
| 45 | } |
| 46 | |