This repository has been archived by the owner on May 24, 2022. It is now read-only.
forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.go
148 lines (121 loc) · 3.09 KB
/
mail.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
147
148
package mail
import (
"bytes"
"fmt"
"log"
"net/smtp"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/template"
)
// A Message represents an email message. Addresses may be of any
// form permitted by RFC 822.
type Message struct {
Sender string
ReplyTo string // may be empty
To string
Subject string
Body string
}
// Sends a activation email to the User.
func SendActivation(to string, data interface{}) error {
msg := Message{}
msg.Subject = "[drone.io] Account Activation"
msg.To = to
var buf bytes.Buffer
err := template.ExecuteTemplate(&buf, "activation.html", &data)
if err != nil {
log.Print(err)
return err
}
msg.Body = buf.String()
return Send(&msg)
}
// Sends an invitation to join a Team
func SendInvitation(team, to string, data interface{}) error {
msg := Message{}
msg.Subject = "Invited to join " + team
msg.To = to
var buf bytes.Buffer
err := template.ExecuteTemplate(&buf, "invitation.html", &data)
if err != nil {
log.Print(err)
return err
}
msg.Body = buf.String()
return Send(&msg)
}
// Sends an email to the User's email address
// with Password reset information.
func SendPassword(to string, data interface{}) error {
msg := Message{}
msg.Subject = "[drone.io] Password Reset"
msg.To = to
var buf bytes.Buffer
err := template.ExecuteTemplate(&buf, "reset_password.html", &data)
if err != nil {
log.Print(err)
return err
}
msg.Body = buf.String()
return Send(&msg)
}
// Sends a build success email to the user.
func SendSuccess(repo, sha, to string, data interface{}) error {
msg := Message{}
msg.Subject = fmt.Sprintf("[%s] SUCCESS building %s", repo, sha)
msg.To = to
var buf bytes.Buffer
err := template.ExecuteTemplate(&buf, "success.html", &data)
if err != nil {
log.Print(err)
return err
}
msg.Body = buf.String()
return Send(&msg)
}
// Sends a build failure email to the user.
func SendFailure(repo, sha, to string, data interface{}) error {
msg := Message{}
msg.Subject = fmt.Sprintf("[%s] FAILURE building %s", repo, sha)
msg.To = to
var buf bytes.Buffer
err := template.ExecuteTemplate(&buf, "failure.html", &data)
if err != nil {
log.Print(err)
return err
}
msg.Body = buf.String()
return Send(&msg)
}
// Send sends an email message.
func Send(msg *Message) error {
// retieve the system settings from the database
// so that we can get the SMTP details.
s, err := database.GetSettings()
if err != nil {
log.Print(err)
return err
}
// set the FROM address
msg.Sender = s.SmtpAddress
// format the raw email message body
body := fmt.Sprintf(emailTemplate, msg.Sender, msg.To, msg.Subject, msg.Body)
var auth smtp.Auth
if len(s.SmtpUsername) > 0 {
auth = smtp.PlainAuth("", s.SmtpUsername, s.SmtpPassword, s.SmtpServer)
}
addr := fmt.Sprintf("%s:%s", s.SmtpServer, s.SmtpPort)
err = smtp.SendMail(addr, auth, msg.Sender, []string{msg.To}, []byte(body))
if err != nil {
log.Print(err)
return err
}
return nil
}
// Text-template used to generate a raw Email message
var emailTemplate = `From: %s
To: %s
Subject: %s
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"
%s`