switch to different mail lib to add insecure flag

This commit is contained in:
Willy Kloucek
2022-07-25 16:22:27 +02:00
parent 589a047471
commit 26d50b5623
5 changed files with 50 additions and 11 deletions
@@ -3,7 +3,7 @@ package channels
import (
"context"
"net/smtp"
"crypto/tls"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
groups "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
@@ -12,6 +12,7 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/notifications/pkg/config"
"github.com/pkg/errors"
mail "github.com/xhit/go-simple-mail/v2"
)
// Channel defines the methods of a communication channel.
@@ -29,6 +30,7 @@ func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) {
logger.Error().Err(err).Msg("could not get gateway client")
return nil, err
}
return Mail{
gatewayClient: gc,
conf: cfg,
@@ -36,7 +38,7 @@ func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) {
}, nil
}
// Mail is the communcation channel for email.
// Mail is the communication channel for email.
type Mail struct {
gatewayClient gateway.GatewayAPIClient
conf config.Config
@@ -45,17 +47,39 @@ type Mail struct {
// SendMessage sends a message to all given users.
func (m Mail) SendMessage(userIDs []string, msg string) error {
if m.conf.Notifications.SMTP.Host == "" {
return nil
}
to, err := m.getReceiverAddresses(userIDs)
if err != nil {
return err
}
body := []byte(msg)
smtpConf := m.conf.Notifications.SMTP
auth := smtp.PlainAuth("", smtpConf.Sender, smtpConf.Password, smtpConf.Host)
if err := smtp.SendMail(smtpConf.Host+":"+smtpConf.Port, auth, smtpConf.Sender, to, body); err != nil {
return errors.Wrap(err, "could not send mail")
server := mail.NewSMTPClient()
server.Host = m.conf.Notifications.SMTP.Host
server.Port = m.conf.Notifications.SMTP.Port
server.Username = m.conf.Notifications.SMTP.Sender
server.Password = m.conf.Notifications.SMTP.Password
server.TLSConfig = &tls.Config{InsecureSkipVerify: m.conf.Notifications.SMTP.Insecure}
smtpClient, err := server.Connect()
if err != nil {
return err
}
email := mail.NewMSG()
email.SetFrom(m.conf.Notifications.SMTP.Sender).
AddTo(to...)
email.SetBody(mail.TextPlain, msg)
err = email.Send(smtpClient)
if err != nil {
return err
}
return nil
}
+3 -2
View File
@@ -31,14 +31,15 @@ type Notifications struct {
// SMTP combines the smtp configuration options.
type SMTP struct {
Host string `yaml:"smtp_host" env:"NOTIFICATIONS_SMTP_HOST" desc:"SMTP host to connect to."`
Port string `yaml:"smtp_port" env:"NOTIFICATIONS_SMTP_PORT" desc:"Port of the SMTP host to connect to."`
Port int `yaml:"smtp_port" env:"NOTIFICATIONS_SMTP_PORT" desc:"Port of the SMTP host to connect to."`
Sender string `yaml:"smtp_sender" env:"NOTIFICATIONS_SMTP_SENDER" desc:"Sender of emails that will be sent."`
Password string `yaml:"smtp_password" env:"NOTIFICATIONS_SMTP_PASSWORD" desc:"Password of the SMTP host to connect to."`
Insecure bool `yaml:"insecure" env:"NOTIFICATIONS_SMTP_INSECURE" desc:"Allow insecure connections to the SMTP server."`
}
// Events combines the configuration options for the event bus.
type Events struct {
Endpoint string `yaml:"endpoint" env:"NOTIFICATIONS_EVENTS_ENDPOINT" desc:"The address of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture.`
Endpoint string `yaml:"endpoint" env:"NOTIFICATIONS_EVENTS_ENDPOINT" desc:"The address of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture."`
Cluster string `yaml:"cluster" env:"NOTIFICATIONS_EVENTS_CLUSTER" desc:"The clusterID of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture. Mandatory when using NATS as event system."`
ConsumerGroup string `yaml:"group" env:"NOTIFICATIONS_EVENTS_GROUP" desc:"Name of the event group / queue on the event system."`
}
@@ -24,8 +24,8 @@ func DefaultConfig() *config.Config {
},
Notifications: config.Notifications{
SMTP: config.SMTP{
Host: "127.0.0.1",
Port: "1025",
Host: "",
Port: 1025,
Sender: "noreply@example.com",
},
Events: config.Events{