-
Notifications
You must be signed in to change notification settings - Fork 2
/
simplemailer.go
88 lines (76 loc) · 2.36 KB
/
simplemailer.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
package SimpleMailer
var configs Config
// configs for SMTP login information and email template directory
type Config struct {
SMTPuser string
SMTPpass string
SMTPhost string
SMTPname string
SMTPfrom string
SMTPport string
EmailsDir string
}
// variables in an array that will be replaced inside template
// USERNAME with text: {{USERNAME}} in the .html template
type Variables struct {
Inputs map[string]interface{}
}
// Array of email addresses, subject for email, and .html template
type BulkSend struct {
Emails []string
Subject string
Template string
}
// A single email address, subject, .html template and array of Variables
type Outgoing struct {
Email string
Subject string
Template string
Variables Variables
}
// function to set the SMTP login information and email directory
// be sure to leave forward slash on end of email directory
func SetSMTPInfo(host string, port string, user string, password string, fromName string, fromAddress string, emailsDir string){
configs = Config{
SMTPhost: host,
SMTPuser: user,
SMTPpass: password,
SMTPname: fromName,
SMTPfrom: fromAddress,
SMTPport: port,
EmailsDir: emailsDir }
}
// function to send a single email from Outgoing struct
func SendSingle(outgoing Outgoing) bool {
chk := SendEmail(outgoing)
return chk
}
// function to send multiple emails without any variables.
// good for sending a flat html email to many. uses BulkSend struct
func SendBulkEmails(outgoing BulkSend) []map[string]interface{} {
var response []map[string]interface{}
for _,sendEmail := range outgoing.Emails {
sendSingle := Outgoing{Email: sendEmail, Subject: outgoing.Subject, Template: outgoing.Template, Variables: Variables{}}
success := SendEmail(sendSingle)
thisResponse := map[string]interface{}{"email": sendEmail, "status": success}
response = append(response, thisResponse)
}
return response
}
// function to send multiple emails with variables.
// uses and array of Outgoing structs
func SendMultiple(outgoing []Outgoing) []map[string]interface{} {
var response []map[string]interface{}
for _,sendEmail := range outgoing {
success := SendEmail(sendEmail)
thisResponse := map[string]interface{}{"email": sendEmail.Email, "status": success}
response = append(response, thisResponse)
}
return response
}
// simple check error panic function
func checkErr(err error) {
if err != nil {
panic(err)
}
}