-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
email.go
90 lines (72 loc) · 2.32 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
// Package sender provides email sender
package sender
import (
"time"
"github.com/go-pkgz/auth/logger"
"github.com/go-pkgz/email"
)
// Email implements sender interface for VerifyHandler
// Uses common subject line and "from" for all messages
type Email struct {
EmailParams
logger.L
sender *email.Sender
}
// EmailParams with all needed to make new Email client with smtp
type EmailParams struct {
Host string // SMTP host
Port int // SMTP port
From string // From email field
Subject string // Email subject
ContentType string // Content type
TLS bool // TLS auth
StartTLS bool // StartTLS auth
InsecureSkipVerify bool // Skip certificate verification
Charset string // Character set
LoginAuth bool // LOGIN auth method instead of default PLAIN, needed for Office 365 and outlook.com
SMTPUserName string // username
SMTPPassword string // password
TimeOut time.Duration // TCP connection timeout
}
// NewEmailClient creates email client
func NewEmailClient(emailParams EmailParams, l logger.L) *Email {
var opts []email.Option
if emailParams.SMTPUserName != "" {
opts = append(opts, email.Auth(emailParams.SMTPUserName, emailParams.SMTPPassword))
}
if emailParams.ContentType != "" {
opts = append(opts, email.ContentType(emailParams.ContentType))
}
if emailParams.Charset != "" {
opts = append(opts, email.Charset(emailParams.Charset))
}
if emailParams.LoginAuth {
opts = append(opts, email.LoginAuth())
}
if emailParams.Port != 0 {
opts = append(opts, email.Port(emailParams.Port))
}
if emailParams.TimeOut != 0 {
opts = append(opts, email.TimeOut(emailParams.TimeOut))
}
if emailParams.TLS {
opts = append(opts, email.TLS(true))
}
if emailParams.StartTLS {
opts = append(opts, email.STARTTLS(true))
}
if emailParams.InsecureSkipVerify {
opts = append(opts, email.InsecureSkipVerify(true))
}
sender := email.NewSender(emailParams.Host, opts...)
return &Email{EmailParams: emailParams, L: l, sender: sender}
}
// Send email with given text
func (e *Email) Send(to, text string) error {
e.Logf("[DEBUG] send %q to %s", text, to)
return e.sender.Send(text, email.Params{
From: e.From,
To: []string{to},
Subject: e.Subject,
})
}