Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

experimental: add aws ses as email sender

goenning0df7ac2aa213
diff --git a/app/cmd/server.go b/app/cmd/server.go
index 9d99f3ee3..23125f048 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -21,6 +21,7 @@ import (
_ "github.com/getfider/fider/app/services/blob/fs"
_ "github.com/getfider/fider/app/services/blob/s3"
_ "github.com/getfider/fider/app/services/blob/sql"
+ _ "github.com/getfider/fider/app/services/email/awsses"
_ "github.com/getfider/fider/app/services/email/mailgun"
_ "github.com/getfider/fider/app/services/email/smtp"
_ "github.com/getfider/fider/app/services/httpclient"
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index a11e66f03..ac2b3f8f4 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -82,13 +82,19 @@ type config struct {
}
}
Email struct {
+ Type string `env:"EMAIL"` // possible values: smtp, mailgun, awsses
NoReply string `env:"EMAIL_NOREPLY,required"`
Allowlist string `env:"EMAIL_ALLOWLIST"`
Blocklist string `env:"EMAIL_BLOCKLIST"`
- Mailgun struct {
+ AWSSES struct {
+ Region string `env:"EMAIL_AWSSES_REGION"`
+ AccessKeyID string `env:"EMAIL_AWSSES_ACCESS_KEY_ID"`
+ SecretAccessKey string `env:"EMAIL_AWSSES_SECRET_ACCESS_KEY"`
+ }
+ Mailgun struct {
APIKey string `env:"EMAIL_MAILGUN_API"`
Domain string `env:"EMAIL_MAILGUN_DOMAIN"`
- Region string `env:"EMAIL_MAILGUN_REGION,default=US"`
+ Region string `env:"EMAIL_MAILGUN_REGION,default=US"` // possible values: US or EU
}
SMTP struct {
Host string `env:"EMAIL_SMTP_HOST"`
@@ -99,7 +105,7 @@ type config struct {
}
}
BlobStorage struct {
- Type string `env:"BLOB_STORAGE,default=sql"`
+ Type string `env:"BLOB_STORAGE,default=sql"` // possible values: sql, fs or s3
S3 struct {
EndpointURL string `env:"BLOB_STORAGE_S3_ENDPOINT_URL"`
Region string `env:"BLOB_STORAGE_S3_REGION"`
@@ -134,14 +140,31 @@ func Reload() {
panic(errors.Wrap(err, "failed to parse environment variables"))
}
- if Config.Email.Mailgun.APIKey != "" {
+ // Email Type can be inferred if absense
+ if Config.Email.Type == "" {
+ if Config.Email.Mailgun.APIKey != "" {
+ Config.Email.Type = "mailgun"
+ } else if Config.Email.AWSSES.AccessKeyID != "" {
+ Config.Email.Type = "awsses"
+ } else {
+ Config.Email.Type = "smtp"
+ }
+ }
+
+ emailType := Config.Email.Type
+ if emailType == "mailgun" {
+ mustBeSet("EMAIL_MAILGUN_API")
mustBeSet("EMAIL_MAILGUN_DOMAIN")
- } else {
+ } else if emailType == "awsses" {
+ mustBeSet("EMAIL_AWSSES_REGION")
+ mustBeSet("EMAIL_AWSSES_ACCESS_KEY_ID")
+ mustBeSet("EMAIL_AWSSES_SECRET_ACCESS_KEY")
+ } else if emailType == "smtp" {
mustBeSet("EMAIL_SMTP_HOST")
mustBeSet("EMAIL_SMTP_PORT")
}
- bsType := strings.ToLower(Config.BlobStorage.Type)
+ bsType := Config.BlobStorage.Type
if bsType == "s3" {
mustBeSet("BLOB_STORAGE_S3_BUCKET")
} else if bsType == "fs" {
diff --git a/app/services/email/awsses/awsses.go b/app/services/email/awsses/awsses.go
new file mode 100644
index 000000000..b7e20f4b7
--- /dev/null
+++ b/app/services/email/awsses/awsses.go
@@ -0,0 +1,134 @@
+package awsses
+
+import (
+ "context"
+
+ "github.com/aws/aws-sdk-go/aws"
+ "github.com/aws/aws-sdk-go/aws/credentials"
+ "github.com/aws/aws-sdk-go/aws/session"
+ ses "github.com/aws/aws-sdk-go/service/sesv2"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/pkg/log"
+ "github.com/getfider/fider/app/services/email"
+)
+
+var sesClient *ses.SESV2
+
+func init() {
+ bus.Register(Service{})
+}
+
+type Service struct{}
+
+func (s Service) Name() string {
+ return "AWSSES"
+}
+
+func (s Service) Category() string {
+ return "email"
+}
+
+func (s Service) Enabled() bool {
+ return env.Config.Email.Type == "awsses"
+}
+
+func (s Service) Init() {
+ sesEnvConfig := env.Config.Email.AWSSES
+ sesConfig := &aws.Config{
+ Credentials: credentials.NewStaticCredentials(sesEnvConfig.AccessKeyID, sesEnvConfig.SecretAccessKey, ""),
+ Region: aws.String(sesEnvConfig.Region),
+ }
+ awsSession, err := session.NewSession(sesConfig)
+ if err != nil {
+ panic(err)
+ }
+
+ sesClient = ses.New(awsSession)
+ bus.AddListener(sendMail)
+ bus.AddHandler(fetchRecentSupressions)
+}
+
+func sendMail(ctx context.Context, c *cmd.SendMail) {
+ if c.Props == nil {
+ c.Props = dto.Props{}
+ }
+
+ if c.From.Address == "" {
+ c.From.Address = email.NoReply
+ }
+
+ for _, to := range c.To {
+ if to.Address == "" {
+ return
+ }
+
+ if !email.CanSendTo(to.Address) {
+ log.Warnf(ctx, "Skipping email to '@{Name} <@{Address}>'.", dto.Props{
+ "Name": to.Name,
+ "Address": to.Address,
+ })
+ return
+ }
+
+ log.Debugf(ctx, "Sending email to @{Address} with template @{TemplateName} and params @{Props}.", dto.Props{
+ "Address": to.Address,
+ "TemplateName": c.TemplateName,
+ "Props": to.Props,
+ })
+
+ message := email.RenderMessage(ctx, c.TemplateName, c.From.Address, c.Props.Merge(to.Props))
+
+ input := &ses.SendEmailInput{
+ FromEmailAddress: aws.String(c.From.String()),
+ Destination: &ses.Destination{
+ ToAddresses: []*string{
+ aws.String(to.String()),
+ },
+ },
+ Content: &ses.EmailContent{
+ Simple: &ses.Message{
+ Body: &ses.Body{
+ Html: &ses.Content{
+ Charset: aws.String("UTF-8"),
+ Data: aws.String(message.Body),
+ },
+ },
+ Subject: &ses.Content{
+ Charset: aws.String("UTF-8"),
+ Data: aws.String(message.Subject),
+ },
+ },
+ },
+ }
+
+ result, err := sesClient.SendEmailWithContext(ctx, input)
+ if err != nil {
+ panic(errors.Wrap(err, "failed to send email with template %s", c.TemplateName))
+ }
+
+ log.Debugf(ctx, "Email sent with ID @{MessageId}.", dto.Props{
+ "MessageId": *result.MessageId,
+ })
+ }
+}
+
+func fetchRecentSupressions(ctx context.Context, q *query.FetchRecentSupressions) error {
+ response, err := sesClient.ListSuppressedDestinationsWithContext(ctx, &ses.ListSuppressedDestinationsInput{
+ StartDate: aws.Time(q.StartTime),
+ PageSize: aws.Int64(1000),
+ })
+ if err != nil {
+ return errors.Wrap(err, "failed to list supressed destinations")
+ }
+
+ q.EmailAddresses = make([]string, 0)
+ for _, destination := range response.SuppressedDestinationSummaries {
+ q.EmailAddresses = append(q.EmailAddresses, *destination.EmailAddress)
+ }
+ return nil
+}
diff --git a/app/services/email/mailgun/service.go b/app/services/email/mailgun/service.go
index f23a4fcd1..0f9f8d804 100644
--- a/app/services/email/mailgun/service.go
+++ b/app/services/email/mailgun/service.go
@@ -34,7 +34,7 @@ func (s Service) Category() string {
}
func (s Service) Enabled() bool {
- return env.Config.Email.Mailgun.APIKey != ""
+ return env.Config.Email.Type == "mailgun"
}
func (s Service) Init() {
diff --git a/app/services/email/smtp/smtp.go b/app/services/email/smtp/smtp.go
index d20860be1..17559921a 100644
--- a/app/services/email/smtp/smtp.go
+++ b/app/services/email/smtp/smtp.go
@@ -38,7 +38,7 @@ func (s Service) Category() string {
}
func (s Service) Enabled() bool {
- return env.Config.Email.Mailgun.APIKey == ""
+ return env.Config.Email.Type == "smtp"
}
func (s Service) Init() {