-
Notifications
You must be signed in to change notification settings - Fork 2
/
email.go
146 lines (130 loc) · 3.69 KB
/
email.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ses"
)
const (
Sender = "Re.Use.Full <[email protected]>"
CharSet = "UTF-8"
)
type EmailArgs struct {
Link string
Button string
Message string
Preheader string
Sender string
}
//
// func sendLoginEmail(email, link string) error {
// var b bytes.Buffer
// err := tmpl.Execute(&b, EmailArgs{
// Link: link,
// Button: "Login",
// Message: "Please click the following button to login.",
// Preheader: "Your login request",
// })
// if err != nil {
// return err
// }
//
// return sendEmail(email, "Login to Hyprcubd", b.String())
// }
// func sendInviteEmail(email, org, link string) error {
// var b bytes.Buffer
// err := tmpl.Execute(&b, EmailArgs{
// Link: link,
// Button: "Create Account",
// Message: fmt.Sprintf("You have been invited to join %s within Hyprcubd. Please click the following button to setup your account.", org),
// Preheader: "You have been invited to Hyprcubd",
// })
// if err != nil {
// return err
// }
//
// return sendEmail(email, "Welcome to Hyprcubd!", b.String())
// }
func sendNewAccountEmail(email, link string) error {
var b bytes.Buffer
err := t.ExecuteTemplate(&b, "email.tmpl", EmailArgs{
Link: link,
Button: "Complete Registration",
Message: "Your account has been created and we are in the process of reviewing it. Please click the following button to verify your email and set your password.",
Preheader: "New account",
})
if err != nil {
return err
}
return sendEmail(email, "Re.Use.Full Account Registration", b.String())
}
/*
Notifies admin of new organizations pending approval.
*/
func sendAdminNotificationEmail(orgName string) error {
notificationMessage := fmt.Sprintf("%s has completed registration, and is ready for approval. Please log in and visit https://app.reusefull.org/admin to review their listing.", orgName)
return sendEmail("[email protected]", "New Organization Registered", notificationMessage)
}
/*
Sends email containing contact form submission details
*/
func sendContactEmail(recipient string, senderEmail string, senderName string, body string) error {
var b bytes.Buffer
err := t.ExecuteTemplate(&b, "email.tmpl", EmailArgs{
Link: fmt.Sprintf("mailto:%s", senderEmail),
Button: "Respond",
Message: body,
Sender: senderName,
Preheader: "New contact form submission.",
})
if err != nil {
return err
}
err = sendEmail(recipient, "Re.Use.Full Donor Message", b.String())
if err != nil {
return err
}
return nil
}
func sendEmail(email, subject, body string) error {
input := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: []*string{
aws.String(email),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(body),
},
},
Subject: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(subject),
},
},
Source: aws.String(Sender),
}
_, err := sesSvc.SendEmail(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
fmt.Println(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
return err
}
return nil
}