Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit b1bac68

Browse files
committed
started extracting sending email to an interface
1 parent bac9b89 commit b1bac68

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

email/dev.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package email
2+
3+
import (
4+
"fmt"
5+
"staticbackend/internal"
6+
)
7+
8+
type Dev struct{}
9+
10+
func (d Dev) Send(data internal.SendMailData) error {
11+
fmt.Println("====== SENDING EMAIL ======")
12+
fmt.Println("from: ", data.From)
13+
fmt.Println("ReplyTo: ", data.ReplyTo)
14+
fmt.Println("to: ", data.To)
15+
fmt.Println("subject: ", data.Subject)
16+
fmt.Printf("body\n%s\n\n", data.TextBody)
17+
fmt.Println("====== /SENDING EMAIL ======")
18+
return nil
19+
}

email/ses.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package email
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"staticbackend/internal"
7+
"strings"
8+
9+
"github.com/aws/aws-sdk-go/aws"
10+
"github.com/aws/aws-sdk-go/aws/session"
11+
"github.com/aws/aws-sdk-go/service/ses"
12+
13+
"staticbackend/internal"
14+
)
15+
16+
type AWSSES struct{}
17+
18+
func (AWSSES) Send(data internal.SendMailData) error {
19+
if len(data.To) == 0 || strings.Index(data.To, "@") == -1 {
20+
return fmt.Errorf("empty To email")
21+
}
22+
23+
if len(data.ReplyTo) == 0 {
24+
data.ReplyTo = data.From
25+
}
26+
27+
charset := "UTF-8"
28+
29+
sess, err := session.NewSession(&aws.Config{
30+
Region: aws.String(os.Getenv("AWS_REGION"))},
31+
)
32+
if err != nil {
33+
return err
34+
}
35+
36+
// Create an SES session.
37+
svc := ses.New(sess)
38+
39+
// Assemble the email.
40+
input := &ses.SendEmailInput{
41+
Destination: &ses.Destination{
42+
CcAddresses: []*string{},
43+
ToAddresses: []*string{
44+
aws.String(data.To),
45+
},
46+
},
47+
Message: &ses.Message{
48+
Body: &ses.Body{
49+
Html: &ses.Content{
50+
Charset: aws.String(charset),
51+
Data: aws.String(data.HTMLBody),
52+
},
53+
Text: &ses.Content{
54+
Charset: aws.String(charset),
55+
Data: aws.String(data.TextBody),
56+
},
57+
},
58+
Subject: &ses.Content{
59+
Charset: aws.String(charset),
60+
Data: aws.String(data.Subject),
61+
},
62+
},
63+
Source: aws.String(fromEmail),
64+
ReplyToAddresses: aws.StringSlice([]string{data.ReplyTo}),
65+
// Uncomment to use a configuration set
66+
//ConfigurationSetName: aws.String(ConfigurationSet),
67+
}
68+
69+
// Attempt to send the email.
70+
if _, err := svc.SendEmail(input); err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}

internal/mailer.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package internal
2+
3+
// SendMailData contains necessary fields to send an email
4+
type SendMailData struct {
5+
From string
6+
FromName string
7+
To string
8+
ToName string
9+
Subject string
10+
HTMLBody string
11+
TextBody string
12+
ReplyTo string
13+
}
14+
15+
// Mailer is used to have different implementation for sending email
16+
type Mailer interface {
17+
Send(SendMailData) error
18+
}

0 commit comments

Comments
 (0)