forked from semaphoreui/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
67 lines (57 loc) · 1.24 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
package util
import (
"bytes"
log "github.com/Sirupsen/logrus"
"io"
"net/smtp"
)
// SendMail dispatches a mail using smtp
func SendMail(emailHost, mailSender, mailRecipient string, mail bytes.Buffer) error {
c, err := smtp.Dial(emailHost)
if err != nil {
return err
}
defer func(c *smtp.Client) {
err = c.Close()
if err != nil {
log.Error(err)
}
}(c)
// Set the sender and recipient.
err = c.Mail(mailSender)
if err != nil {
return err
}
err = c.Rcpt(mailRecipient)
if err != nil {
return err
}
// Send the email body.
wc, err := c.Data()
if err != nil {
return err
}
defer func(wc io.WriteCloser) {
err = wc.Close()
if err != nil {
log.Error(err)
}
}(wc)
_, err = mail.WriteTo(wc)
return err
}
// SendSecureMail dispatches a mail using smtp with authentication and StartTLS
func SendSecureMail(emailHost, emailPort, mailSender, mailUsername, mailPassword, mailRecipient string, mail bytes.Buffer) error {
// Receiver email address.
to := []string{
mailRecipient,
}
// Authentication.
auth := smtp.PlainAuth("", mailUsername, mailPassword, emailHost)
// Sending email.
err := smtp.SendMail(emailHost+":"+emailPort, auth, mailSender, to, mail.Bytes())
if err != nil {
log.Error(err)
}
return err
}