forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender-smtp.go
129 lines (110 loc) · 3.61 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* 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"
"crypto/tls"
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.in/gomail.v2"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/mailer"
"github.com/pydio/cells/x/configx"
)
type Smtp struct {
User string
Password string
Host string
Port int
LocalName string
InsecureSkipVerify bool
}
func (gm *Smtp) Configure(ctx context.Context, conf configx.Values) error {
gm.User = conf.Val("user").String()
if gm.User == "" {
return fmt.Errorf("cannot configure mailer: missing compulsory user name")
}
gm.Password = conf.Val("clearPass").Default("NOT_SET").String()
if gm.Password == "NOT_SET" {
gm.Password = config.GetSecret(conf.Val("password").String()).Default("NOT_SET").String()
}
if gm.Password == "NOT_SET" {
return fmt.Errorf("cannot configure mailer: missing compulsory password")
}
gm.Host = conf.Val("host").String()
if gm.Host == "" {
return fmt.Errorf("cannot configure mailer: missing compulsory host address")
}
gm.Port = conf.Val("port").Int()
if gm.Port == 0 {
return fmt.Errorf("cannot configure mailer: missing compulsory port")
}
if loc := conf.Val("localName").Default("").String(); loc != "" {
gm.LocalName = loc
}
// Set default to be false.
gm.InsecureSkipVerify = conf.Val("insecureSkipVerify").Bool()
log.Logger(ctx).Debug("SMTP Configured", zap.String("u", gm.User), zap.String("h", gm.Host), zap.Int("p", gm.Port))
return nil
}
func (gm *Smtp) Check(ctx context.Context) error {
// Default value, unnecessary check
if gm.Host == "my.smtp.server" {
return errors.New("mailer not configured yet (my.smtp.server)")
}
// Test Config - Unfortunately we cannot set the Timeout here - 10s by default
d := gomail.NewDialer(gm.Host, gm.Port, gm.User, gm.Password)
if gm.LocalName != "" {
d.LocalName = gm.LocalName
}
tlsConfig := tls.Config{
InsecureSkipVerify: gm.InsecureSkipVerify,
ServerName: gm.Host,
}
// Check configuration
d.TLSConfig = &tlsConfig
if closer, err := d.Dial(); err != nil {
log.Logger(ctx).Warn("Mailer check failed", zap.Error(err))
return err
} else {
log.Logger(ctx).Info("Mailer check passed")
closer.Close()
}
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)
if gm.LocalName != "" {
d.LocalName = gm.LocalName
}
tlsConfig := tls.Config{
InsecureSkipVerify: gm.InsecureSkipVerify,
ServerName: gm.Host,
}
d.TLSConfig = &tlsConfig
return d.DialAndSend(m)
}