-
Notifications
You must be signed in to change notification settings - Fork 168
/
sender-smtp.go
105 lines (92 loc) · 2.9 KB
/
sender-smtp.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package mailer
import (
"context"
"fmt"
"crypto/tls"
"strconv"
"go.uber.org/zap"
gomail "gopkg.in/gomail.v2"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/mailer"
)
type Smtp struct {
User string
Password string
Host string
Port int
InsecureSkipVerify bool
}
func (gm *Smtp) Configure(conf config.Map) error {
val := conf.Get("user")
if val == nil {
return fmt.Errorf("cannot configure mailer: missing compulsory user name")
}
gm.User = val.(string)
if clear := conf.Get("clearPass"); clear != nil {
// Used by tests
gm.Password = clear.(string)
} else {
val = conf.Get("password")
if val == nil {
return fmt.Errorf("cannot configure mailer: missing compulsory password")
}
passwordSecret := val.(string)
gm.Password = config.GetSecret(passwordSecret).String("")
}
val = conf.Get("host")
if val == nil {
return fmt.Errorf("cannot configure mailer: missing compulsory host address")
}
gm.Host = val.(string)
portConf := conf.Get("port")
if portConf == nil {
return fmt.Errorf("cannot configure mailer: missing compulsory port")
}
if sConf, ok := portConf.(string); ok && sConf != "" {
parsed, _ := strconv.ParseInt(sConf, 10, 64)
gm.Port = int(parsed)
} else {
gm.Port = int(portConf.(float64))
}
// Set defaul to be false.
gm.InsecureSkipVerify = false
if conf.Get("insecureSkipVerify") != nil {
gm.InsecureSkipVerify = conf.Get("insecureSkipVerify").(bool)
}
log.Logger(context.Background()).Debug("SMTP Configured", zap.String("u", gm.User), zap.String("h", gm.Host), zap.Int("p", gm.Port))
return nil
}
func (gm *Smtp) Send(email *mailer.Mail) error {
log.Logger(context.Background()).Debug("Trying to send email", zap.String("smtpHost", gm.Host), zap.String("smtpUser", gm.User), zap.Any("mail", email))
m, e := NewGomailMessage(email)
if e != nil {
return e
}
d := gomail.NewDialer(gm.Host, gm.Port, gm.User, gm.Password)
tlsConfig := tls.Config{
InsecureSkipVerify: gm.InsecureSkipVerify,
ServerName: gm.Host,
}
d.TLSConfig = &tlsConfig
return d.DialAndSend(m)
}