forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.go
59 lines (49 loc) · 1.17 KB
/
mailer.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
package mail
import (
"net/smtp"
)
type MailSender interface {
SendMessage(messages ...*Message) (err error)
}
type Mailer struct {
Server string
Port int
UserName string
Password string
Host string // This is optional, only used if you want to tell smtp server your hostname
Auth smtp.Auth // This is optional, only used if Authentication is not plain
Sender *Sender // This is optional, only used if the From/ReplyTo is not specified in the message
}
type Sender struct {
From string
ReplyTo string
}
// Send the given email messages using this Mailer.
func (m *Mailer) SendMessage(messages ...*Message) (err error) {
if m.Auth == nil {
m.Auth = smtp.PlainAuth(m.UserName, m.UserName, m.Password, m.Server)
}
c, err := Transport(m.Server, m.Port, m.Host, m.Auth)
if err != nil {
return
}
defer c.Quit()
for _, message := range messages {
m.fillDefault(message)
if err = Send(c, message); err != nil {
return
}
}
return
}
func (m *Mailer) fillDefault(message *Message) {
if m.Sender == nil {
return
}
if message.From == "" {
message.From = m.Sender.From
}
if message.ReplyTo == "" {
message.ReplyTo = m.Sender.ReplyTo
}
}