Skip to content

Commit

Permalink
support for SendGrid payments/balance emails
Browse files Browse the repository at this point in the history
  • Loading branch information
ponthief committed May 1, 2023
1 parent 24082c8 commit 77f1de8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
2 changes: 2 additions & 0 deletions docs/SETTINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ Here are the descriptions of values available to use in the `settings` table:
| FUNCTION_LNDHUB | DISABLE | system level switch for using LNDHUB in place of LND |
| LNDHUB_URL | | URL for the LNDHUB service |
| FUNCTION_INTERNAL_API | DISABLE | system level switch for activating the internal API |
| SENDGRID_API_KEY | | User API Key from SendGrid.com |
| SENDGRID_EMAIL_SENDER | | Single Sender email address verified by SendGrid |
126 changes: 75 additions & 51 deletions email/email.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package email

import (
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/boltcard/boltcard/db"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
)

func Send_balance_email(recipient_email string, card_id int) {
Expand Down Expand Up @@ -86,70 +89,91 @@ func Send_balance_email(recipient_email string, card_id int) {
}

// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/ses-example-send-email.html
// https://github.com/sendgrid/sendgrid-go

func Send_email(recipient string, subject string, htmlBody string, textBody string) {

aws_ses_id := db.Get_setting("AWS_SES_ID")
aws_ses_secret := db.Get_setting("AWS_SES_SECRET")
sender := db.Get_setting("AWS_SES_EMAIL_FROM")
region := db.Get_setting("AWS_REGION")
send_grid_api_key := db.Get_setting("SENDGRID_API_KEY")
send_grid_email_sender := db.Get_setting("SENDGRID_EMAIL_SENDER")
if send_grid_api_key != "" && send_grid_email_sender != "" {
from := mail.NewEmail("", send_grid_email_sender)
subject := subject
to := mail.NewEmail("", recipient)
plainTextContent := textBody
htmlContent := htmlBody
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(send_grid_api_key)
response, err := client.Send(message)
if err != nil {
log.Warn(err.Error())
} else {
log.WithFields(log.Fields{"result": response}).Info("email sent")
}

} else {

sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(aws_ses_id, aws_ses_secret, ""),
})
aws_ses_id := db.Get_setting("AWS_SES_ID")
aws_ses_secret := db.Get_setting("AWS_SES_SECRET")
sender := db.Get_setting("AWS_SES_EMAIL_FROM")
region := db.Get_setting("AWS_REGION")

svc := ses.New(sess)
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(aws_ses_id, aws_ses_secret, ""),
})

charSet := "UTF-8"
svc := ses.New(sess)

input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
aws.String(recipient),
charSet := "UTF-8"

input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
aws.String(recipient),
},
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(htmlBody),
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(htmlBody),
},
Text: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(textBody),
},
},
Text: &ses.Content{
Subject: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(textBody),
Data: aws.String(subject),
},
},
Subject: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(subject),
},
},
Source: aws.String(sender),
//ConfigurationSetName: aws.String(ConfigurationSet),
}

result, err := svc.SendEmail(input)
Source: aws.String(sender),
//ConfigurationSetName: aws.String(ConfigurationSet),
}

if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
log.Warn(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
log.Warn(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
log.Warn(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
log.Warn(aerr.Error())
result, err := svc.SendEmail(input)

if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
log.Warn(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
log.Warn(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
log.Warn(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
log.Warn(aerr.Error())
}
} else {
log.Warn(err.Error())
}
} else {
log.Warn(err.Error())

return
}

return
log.WithFields(log.Fields{"result": result}).Info("email sent")
}

log.WithFields(log.Fields{"result": result}).Info("email sent")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/sendgrid/rest v2.6.9+incompatible // indirect
github.com/sendgrid/sendgrid-go v3.12.0+incompatible // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
github.com/sendgrid/rest v2.6.9+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/sendgrid/sendgrid-go v3.12.0+incompatible h1:/N2vx18Fg1KmQOh6zESc5FJB8pYwt5QFBDflYPh1KVg=
github.com/sendgrid/sendgrid-go v3.12.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
2 changes: 2 additions & 0 deletions sql/settings.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ INSERT INTO settings (name, value) VALUES ('EMAIL_MAX_TXS', '');
INSERT INTO settings (name, value) VALUES ('FUNCTION_LNDHUB', '');
INSERT INTO settings (name, value) VALUES ('LNDHUB_URL', '');
INSERT INTO settings (name, value) VALUES ('FUNCTION_INTERNAL_API', '');
INSERT INTO settings (name, value) VALUES ('SENDGRID_API_KEY', '');
INSERT INTO settings (name, value) VALUES ('SENDGRID_EMAIL_SENDER', '');

0 comments on commit 77f1de8

Please sign in to comment.