-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailgun.go
115 lines (96 loc) · 3.18 KB
/
mailgun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package signup
import (
"context"
"fmt"
"os"
"time"
"github.com/mailgun/mailgun-go/v4"
)
type MailgunService struct {
domain string // Mail domain name.
defaultSender string // Default sender email address.
defaultTemplate string // Default email template use when calling SendWelcome().
mgClient *mailgun.MailgunImpl // Mailgun API Client
}
func NewMailgunService(domain, apiKey, baseAPIurlOverride string) *MailgunService {
mgClient := mailgun.NewMailgun(domain, apiKey)
if len(baseAPIurlOverride) > 0 {
mgClient.SetAPIBase(baseAPIurlOverride)
}
return &MailgunService{
domain: domain,
defaultSender: fmt.Sprintf("Operation Spark <admissions@%s>", domain),
defaultTemplate: "info-session-signup",
mgClient: mgClient,
}
}
// IsRequired returns true because the email needs to be sent to the student to that they can attend the info session.
func (m MailgunService) isRequired() bool {
return true
}
func (m MailgunService) run(ctx context.Context, su Signup) error {
return m.sendWelcome(ctx, su)
}
func (m MailgunService) name() string {
return "mailgun service"
}
func (m MailgunService) sendWelcome(ctx context.Context, su Signup) error {
isStagingEnv := os.Getenv("APP_ENV") == "staging"
vars, err := su.welcomeData()
if err != nil {
return fmt.Errorf("welcomeData: %w", err)
}
t := mgTemplate{
name: m.defaultTemplate,
variables: map[string]interface{}{
"firstName": vars.FirstName,
"lastName": vars.LastName,
"sessionTime": vars.SessionTime,
"sessionDate": vars.SessionDate,
"joinCode": vars.JoinCode,
"zoomURL": vars.ZoomURL,
"locationLine1": vars.LocationLine1,
"locationCityStateZip": vars.LocationCityStateZip,
"locationMapUrl": vars.LocationMapURL,
"isGmail": vars.IsGmail,
"greenlightEnrollUrl": vars.GreenlightEnrollURL,
},
}
if su.LocationType == "HYBRID" {
t.name = "info-session-signup-hybrid"
}
if isStagingEnv {
t.version = "dev"
}
return m.sendWithTemplate(ctx, t, su.Email)
}
type mgTemplate struct {
name string // Name of mailgun template.
variables map[string]interface{} // KV pairs of variables used in the email template.
version string // Mailgun template version. If not set, the active version is used.
}
func (m MailgunService) sendWithTemplate(ctx context.Context, t mgTemplate, recipient string) error {
sender := m.defaultSender
subject := "Welcome from Operation Spark!"
// Empty body because we're using a template
body := ""
message := m.mgClient.NewMessage(sender, subject, body, recipient)
message.SetTemplate(t.name)
if len(t.version) > 0 {
message.SetTemplateVersion(t.version)
}
for k, v := range t.variables {
err := message.AddTemplateVariable(k, v)
if err != nil {
return fmt.Errorf("add template variable: %w ", err)
}
}
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
// Send the message with a 10 second timeout
_, _, err := m.mgClient.Send(ctxWithTimeout, message)
if err != nil {
return fmt.Errorf("send: %w", err)
}
return nil
}