mirror of
https://github.com/nikoksr/notify.git
synced 2024-12-12 10:13:59 +02:00
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package amazonses
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
"github.com/aws/aws-sdk-go-v2/service/ses"
|
|
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// AmazonSES struct holds necessary data to communicate with the Amazon Simple Email Service API.
|
|
type AmazonSES struct {
|
|
client *ses.Client
|
|
senderAddress *string
|
|
receiverAddresses []string
|
|
}
|
|
|
|
// New returns a new instance of a AmazonSES notification service.
|
|
// You will need an Amazon Simple Email Service API access key and secret.
|
|
// See https://aws.github.io/aws-sdk-go-v2/docs/getting-started/
|
|
func New(accessKeyID, secretKey, region, senderAddress string) (*AmazonSES, error) {
|
|
credProvider := credentials.NewStaticCredentialsProvider(accessKeyID, secretKey, "")
|
|
|
|
cfg, err := config.LoadDefaultConfig(
|
|
context.Background(),
|
|
config.WithCredentialsProvider(credProvider),
|
|
config.WithRegion(region),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &AmazonSES{
|
|
client: ses.NewFromConfig(cfg),
|
|
senderAddress: aws.String(senderAddress),
|
|
receiverAddresses: []string{},
|
|
}, nil
|
|
}
|
|
|
|
// AddReceivers takes email addresses and adds them to the internal address list. The Send method will send
|
|
// a given message to all those addresses.
|
|
func (a *AmazonSES) AddReceivers(addresses ...string) {
|
|
a.receiverAddresses = append(a.receiverAddresses, addresses...)
|
|
}
|
|
|
|
// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
|
|
// html as markup language.
|
|
func (a AmazonSES) Send(ctx context.Context, subject, message string) error {
|
|
input := &ses.SendEmailInput{
|
|
Source: a.senderAddress,
|
|
Destination: &types.Destination{
|
|
ToAddresses: a.receiverAddresses,
|
|
},
|
|
Message: &types.Message{
|
|
Body: &types.Body{
|
|
Html: &types.Content{
|
|
Data: aws.String(message),
|
|
},
|
|
// Text: &types.Content{
|
|
// Data: aws.String(message),
|
|
// },
|
|
},
|
|
Subject: &types.Content{
|
|
Data: aws.String(subject),
|
|
},
|
|
},
|
|
}
|
|
|
|
_, err := a.client.SendEmail(ctx, input)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to send mail using Amazon SES service")
|
|
}
|
|
|
|
return nil
|
|
}
|