2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program 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, version 3.
|
|
|
|
//
|
|
|
|
// This program 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
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
// Package smtp provides supports for sending emails
|
|
|
|
package smtp
|
|
|
|
|
|
|
|
import (
|
2021-10-02 20:25:41 +00:00
|
|
|
"bytes"
|
2023-01-15 14:28:31 +00:00
|
|
|
"context"
|
2021-09-26 18:25:37 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-02 20:25:41 +00:00
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
2023-02-19 18:03:45 +00:00
|
|
|
"sync"
|
2021-09-26 18:25:37 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-30 17:11:28 +00:00
|
|
|
"github.com/rs/xid"
|
2023-01-15 14:28:31 +00:00
|
|
|
"github.com/wneessen/go-mail"
|
2021-09-26 18:25:37 +00:00
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
2023-06-03 14:17:32 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2023-03-22 18:02:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2021-09-26 18:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "smtp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EmailContentType defines the support content types for email body
|
|
|
|
type EmailContentType int
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
// Supported email body content type
|
2021-09-26 18:25:37 +00:00
|
|
|
const (
|
|
|
|
EmailContentTypeTextPlain EmailContentType = iota
|
|
|
|
EmailContentTypeTextHTML
|
|
|
|
)
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
const (
|
2022-12-16 17:51:29 +00:00
|
|
|
templateEmailDir = "email"
|
|
|
|
templatePasswordReset = "reset-password.html"
|
|
|
|
templatePasswordExpiration = "password-expiration.html"
|
2023-02-19 18:03:45 +00:00
|
|
|
dialTimeout = 10 * time.Second
|
2021-10-02 20:25:41 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
var (
|
2023-02-19 18:03:45 +00:00
|
|
|
config = &activeConfig{}
|
|
|
|
initialConfig *Config
|
2021-10-02 20:25:41 +00:00
|
|
|
emailTemplates = make(map[string]*template.Template)
|
2021-09-26 18:25:37 +00:00
|
|
|
)
|
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
type activeConfig struct {
|
|
|
|
sync.RWMutex
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *activeConfig) isEnabled() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
return c.config != nil && c.config.Host != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *activeConfig) Set(cfg *dataprovider.SMTPConfigs) {
|
|
|
|
var config *Config
|
|
|
|
if cfg != nil {
|
|
|
|
config = &Config{
|
|
|
|
Host: cfg.Host,
|
|
|
|
Port: cfg.Port,
|
|
|
|
From: cfg.From,
|
|
|
|
User: cfg.User,
|
|
|
|
Password: cfg.Password.GetPayload(),
|
|
|
|
AuthType: cfg.AuthType,
|
|
|
|
Encryption: cfg.Encryption,
|
|
|
|
Domain: cfg.Domain,
|
2023-05-30 17:11:28 +00:00
|
|
|
Debug: cfg.Debug,
|
2023-06-03 14:17:32 +00:00
|
|
|
OAuth2: OAuth2Config{
|
|
|
|
Provider: cfg.OAuth2.Provider,
|
|
|
|
Tenant: cfg.OAuth2.Tenant,
|
|
|
|
ClientID: cfg.OAuth2.ClientID,
|
|
|
|
ClientSecret: cfg.OAuth2.ClientSecret.GetPayload(),
|
|
|
|
RefreshToken: cfg.OAuth2.RefreshToken.GetPayload(),
|
|
|
|
},
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
2023-06-03 14:17:32 +00:00
|
|
|
config.OAuth2.initialize()
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if config != nil && config.Host != "" {
|
|
|
|
if c.config != nil && c.config.isEqual(config) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.config = config
|
|
|
|
logger.Info(logSender, "", "activated new config, server %s:%d", c.config.Host, c.config.Port)
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "activating initial config")
|
|
|
|
c.config = initialConfig
|
|
|
|
if c.config == nil || c.config.Host == "" {
|
|
|
|
logger.Debug(logSender, "", "configuration disabled, email capabilities will not be available")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:55:27 +00:00
|
|
|
func (c *activeConfig) getSMTPClientAndMsg(to, bcc []string, subject, body string, contentType EmailContentType,
|
2023-02-19 18:03:45 +00:00
|
|
|
attachments ...*mail.File,
|
|
|
|
) (*mail.Client, *mail.Msg, error) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
if c.config == nil || c.config.Host == "" {
|
|
|
|
return nil, nil, errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:55:27 +00:00
|
|
|
return c.config.getSMTPClientAndMsg(to, bcc, subject, body, contentType, attachments...)
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 17:55:27 +00:00
|
|
|
func (c *activeConfig) sendEmail(to, bcc []string, subject, body string, contentType EmailContentType, attachments ...*mail.File) error {
|
|
|
|
client, msg, err := c.getSMTPClientAndMsg(to, bcc, subject, body, contentType, attachments...)
|
2023-02-19 18:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), dialTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
return client.DialAndSendWithContext(ctx, msg)
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
// IsEnabled returns true if an SMTP server is configured
|
|
|
|
func IsEnabled() bool {
|
2023-02-19 18:03:45 +00:00
|
|
|
return config.isEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate sets the specified config as active
|
|
|
|
func Activate(c *dataprovider.SMTPConfigs) {
|
|
|
|
config.Set(c)
|
2021-10-02 20:25:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
// Config defines the SMTP configuration to use to send emails
|
|
|
|
type Config struct {
|
|
|
|
// Location of SMTP email server. Leavy empty to disable email sending capabilities
|
|
|
|
Host string `json:"host" mapstructure:"host"`
|
|
|
|
// Port of SMTP email server
|
|
|
|
Port int `json:"port" mapstructure:"port"`
|
2021-10-02 20:25:41 +00:00
|
|
|
// From address, for example "SFTPGo <sftpgo@example.com>".
|
|
|
|
// Many SMTP servers reject emails without a `From` header so, if not set,
|
|
|
|
// SFTPGo will try to use the username as fallback, this may or may not be appropriate
|
2021-09-26 18:25:37 +00:00
|
|
|
From string `json:"from" mapstructure:"from"`
|
|
|
|
// SMTP username
|
|
|
|
User string `json:"user" mapstructure:"user"`
|
|
|
|
// SMTP password. Leaving both username and password empty the SMTP authentication
|
|
|
|
// will be disabled
|
|
|
|
Password string `json:"password" mapstructure:"password"`
|
|
|
|
// 0 Plain
|
|
|
|
// 1 Login
|
|
|
|
// 2 CRAM-MD5
|
2023-06-03 14:17:32 +00:00
|
|
|
// 3 OAuth2
|
2021-09-26 18:25:37 +00:00
|
|
|
AuthType int `json:"auth_type" mapstructure:"auth_type"`
|
|
|
|
// 0 no encryption
|
|
|
|
// 1 TLS
|
|
|
|
// 2 start TLS
|
|
|
|
Encryption int `json:"encryption" mapstructure:"encryption"`
|
|
|
|
// Domain to use for HELO command, if empty localhost will be used
|
|
|
|
Domain string `json:"domain" mapstructure:"domain"`
|
2021-10-02 20:25:41 +00:00
|
|
|
// Path to the email templates. This can be an absolute path or a path relative to the config dir.
|
|
|
|
// Templates are searched within a subdirectory named "email" in the specified path
|
|
|
|
TemplatesPath string `json:"templates_path" mapstructure:"templates_path"`
|
2023-05-30 17:11:28 +00:00
|
|
|
// Set to 1 to enable debug logs
|
|
|
|
Debug int `json:"debug" mapstructure:"debug"`
|
2023-06-03 14:17:32 +00:00
|
|
|
// OAuth2 related settings
|
|
|
|
OAuth2 OAuth2Config `json:"oauth2" mapstructure:"oauth2"`
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
func (c *Config) isEqual(other *Config) bool {
|
|
|
|
if c.Host != other.Host {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Port != other.Port {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.From != other.From {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.User != other.User {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Password != other.Password {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.AuthType != other.AuthType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Encryption != other.Encryption {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Domain != other.Domain {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-30 17:11:28 +00:00
|
|
|
if c.Debug != other.Debug {
|
|
|
|
return false
|
|
|
|
}
|
2023-06-03 14:17:32 +00:00
|
|
|
return c.OAuth2.isEqual(&other.OAuth2)
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:31:02 +00:00
|
|
|
func (c *Config) validate() error {
|
|
|
|
if c.Port <= 0 || c.Port > 65535 {
|
|
|
|
return fmt.Errorf("smtp: invalid port %d", c.Port)
|
|
|
|
}
|
2023-06-03 14:17:32 +00:00
|
|
|
if c.AuthType < 0 || c.AuthType > 3 {
|
2023-03-01 17:31:02 +00:00
|
|
|
return fmt.Errorf("smtp: invalid auth type %d", c.AuthType)
|
|
|
|
}
|
|
|
|
if c.Encryption < 0 || c.Encryption > 2 {
|
|
|
|
return fmt.Errorf("smtp: invalid encryption %d", c.Encryption)
|
|
|
|
}
|
|
|
|
if c.From == "" && c.User == "" {
|
2023-06-03 14:17:32 +00:00
|
|
|
return errors.New(`smtp: from address and user cannot both be empty`)
|
|
|
|
}
|
|
|
|
if c.AuthType == 3 {
|
|
|
|
return c.OAuth2.Validate()
|
2023-03-01 17:31:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) loadTemplates(configDir string) error {
|
2023-02-19 18:03:45 +00:00
|
|
|
if c.TemplatesPath == "" {
|
|
|
|
logger.Debug(logSender, "", "templates path empty, using default")
|
|
|
|
c.TemplatesPath = "templates"
|
|
|
|
}
|
|
|
|
templatesPath := util.FindSharedDataPath(c.TemplatesPath, configDir)
|
|
|
|
if templatesPath == "" {
|
|
|
|
return fmt.Errorf("smtp: invalid templates path %q", templatesPath)
|
|
|
|
}
|
|
|
|
loadTemplates(filepath.Join(templatesPath, templateEmailDir))
|
2023-03-01 17:31:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initialized and validates the SMTP configuration
|
|
|
|
func (c *Config) Initialize(configDir string, isService bool) error {
|
|
|
|
if !isService && c.Host == "" {
|
|
|
|
if err := loadConfigFromProvider(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !config.isEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.loadTemplates(configDir)
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-03-01 17:31:02 +00:00
|
|
|
if err := c.loadTemplates(configDir); err != nil {
|
|
|
|
return err
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-03-01 17:31:02 +00:00
|
|
|
if c.Host == "" {
|
|
|
|
return loadConfigFromProvider()
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-03-01 17:31:02 +00:00
|
|
|
if err := c.validate(); err != nil {
|
|
|
|
return err
|
2023-01-15 14:28:31 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
initialConfig = c
|
|
|
|
config.Set(nil)
|
2023-01-15 14:28:31 +00:00
|
|
|
logger.Debug(logSender, "", "configuration successfully initialized, host: %q, port: %d, username: %q, auth: %d, encryption: %d, helo: %q",
|
2023-02-19 18:03:45 +00:00
|
|
|
c.Host, c.Port, c.User, c.AuthType, c.Encryption, c.Domain)
|
|
|
|
return loadConfigFromProvider()
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 14:28:31 +00:00
|
|
|
func (c *Config) getMailClientOptions() []mail.Option {
|
2023-02-09 08:33:33 +00:00
|
|
|
options := []mail.Option{mail.WithPort(c.Port), mail.WithoutNoop()}
|
2023-01-15 14:28:31 +00:00
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
switch c.Encryption {
|
|
|
|
case 1:
|
2023-01-15 14:28:31 +00:00
|
|
|
options = append(options, mail.WithSSL())
|
2021-09-26 18:25:37 +00:00
|
|
|
case 2:
|
2023-01-15 14:28:31 +00:00
|
|
|
options = append(options, mail.WithTLSPolicy(mail.TLSMandatory))
|
2021-09-26 18:25:37 +00:00
|
|
|
default:
|
2023-01-15 14:28:31 +00:00
|
|
|
options = append(options, mail.WithTLSPolicy(mail.NoTLS))
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
if c.User != "" {
|
|
|
|
options = append(options, mail.WithUsername(c.User))
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
if c.Password != "" {
|
|
|
|
options = append(options, mail.WithPassword(c.Password))
|
2023-01-15 14:28:31 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
if c.User != "" || c.Password != "" {
|
|
|
|
switch c.AuthType {
|
2023-01-15 14:28:31 +00:00
|
|
|
case 1:
|
|
|
|
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthLogin))
|
|
|
|
case 2:
|
|
|
|
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthCramMD5))
|
2023-06-03 14:17:32 +00:00
|
|
|
case 3:
|
|
|
|
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthXOAUTH2))
|
2023-01-15 14:28:31 +00:00
|
|
|
default:
|
|
|
|
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthPlain))
|
|
|
|
}
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
if c.Domain != "" {
|
|
|
|
options = append(options, mail.WithHELO(c.Domain))
|
2023-01-15 14:28:31 +00:00
|
|
|
}
|
2023-05-30 17:11:28 +00:00
|
|
|
if c.Debug > 0 {
|
|
|
|
options = append(options,
|
|
|
|
mail.WithLogger(&logger.MailAdapter{
|
|
|
|
ConnectionID: xid.New().String(),
|
|
|
|
}),
|
|
|
|
mail.WithDebugLog())
|
|
|
|
}
|
2023-01-15 14:28:31 +00:00
|
|
|
return options
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 17:55:27 +00:00
|
|
|
func (c *Config) getSMTPClientAndMsg(to, bcc []string, subject, body string, contentType EmailContentType,
|
2023-02-19 18:03:45 +00:00
|
|
|
attachments ...*mail.File) (*mail.Client, *mail.Msg, error) {
|
2023-03-22 18:02:54 +00:00
|
|
|
version := version.Get()
|
2023-02-19 18:03:45 +00:00
|
|
|
msg := mail.NewMsg()
|
2023-03-22 18:02:54 +00:00
|
|
|
msg.SetUserAgent(fmt.Sprintf("SFTPGo-%s-%s", version.Version, version.CommitHash))
|
2023-02-19 18:03:45 +00:00
|
|
|
|
|
|
|
var from string
|
|
|
|
if c.From != "" {
|
|
|
|
from = c.From
|
|
|
|
} else {
|
|
|
|
from = c.User
|
|
|
|
}
|
|
|
|
if err := msg.From(from); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid from address: %w", err)
|
|
|
|
}
|
|
|
|
if err := msg.To(to...); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-05-25 17:55:27 +00:00
|
|
|
if len(bcc) > 0 {
|
|
|
|
if err := msg.Bcc(bcc...); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
msg.Subject(subject)
|
|
|
|
msg.SetDate()
|
|
|
|
msg.SetMessageID()
|
|
|
|
msg.SetAttachements(attachments)
|
|
|
|
|
|
|
|
switch contentType {
|
|
|
|
case EmailContentTypeTextPlain:
|
|
|
|
msg.SetBodyString(mail.TypeTextPlain, body)
|
|
|
|
case EmailContentTypeTextHTML:
|
|
|
|
msg.SetBodyString(mail.TypeTextHTML, body)
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("smtp: unsupported body content type %v", contentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := mail.NewClient(c.Host, c.getMailClientOptions()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to create mail client: %w", err)
|
|
|
|
}
|
2023-06-03 14:17:32 +00:00
|
|
|
if c.AuthType == 3 {
|
|
|
|
token, err := c.OAuth2.getAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to get oauth2 access token: %w", err)
|
|
|
|
}
|
|
|
|
client.SetPassword(token)
|
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
return client, msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendEmail tries to send an email using the specified parameters
|
2023-05-25 17:55:27 +00:00
|
|
|
func (c *Config) SendEmail(to, bcc []string, subject, body string, contentType EmailContentType, attachments ...*mail.File) error {
|
|
|
|
client, msg, err := c.getSMTPClientAndMsg(to, bcc, subject, body, contentType, attachments...)
|
2023-02-19 18:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), dialTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
return client.DialAndSendWithContext(ctx, msg)
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
func loadTemplates(templatesPath string) {
|
2023-02-19 18:03:45 +00:00
|
|
|
logger.Debug(logSender, "", "loading templates from %q", templatesPath)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
passwordResetPath := filepath.Join(templatesPath, templatePasswordReset)
|
|
|
|
pwdResetTmpl := util.LoadTemplate(nil, passwordResetPath)
|
2022-12-16 17:51:29 +00:00
|
|
|
passwordExpirationPath := filepath.Join(templatesPath, templatePasswordExpiration)
|
|
|
|
pwdExpirationTmpl := util.LoadTemplate(nil, passwordExpirationPath)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
emailTemplates[templatePasswordReset] = pwdResetTmpl
|
2022-12-16 17:51:29 +00:00
|
|
|
emailTemplates[templatePasswordExpiration] = pwdExpirationTmpl
|
2021-10-02 20:25:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// RenderPasswordResetTemplate executes the password reset template
|
2022-05-19 17:49:51 +00:00
|
|
|
func RenderPasswordResetTemplate(buf *bytes.Buffer, data any) error {
|
2023-01-15 14:28:31 +00:00
|
|
|
if !IsEnabled() {
|
2021-11-13 12:25:43 +00:00
|
|
|
return errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
return emailTemplates[templatePasswordReset].Execute(buf, data)
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:51:29 +00:00
|
|
|
// RenderPasswordExpirationTemplate executes the password expiration template
|
|
|
|
func RenderPasswordExpirationTemplate(buf *bytes.Buffer, data any) error {
|
2023-01-15 14:28:31 +00:00
|
|
|
if !IsEnabled() {
|
2022-12-16 17:51:29 +00:00
|
|
|
return errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
return emailTemplates[templatePasswordExpiration].Execute(buf, data)
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
// SendEmail tries to send an email using the specified parameters.
|
2023-05-25 17:55:27 +00:00
|
|
|
func SendEmail(to, bcc []string, subject, body string, contentType EmailContentType, attachments ...*mail.File) error {
|
|
|
|
return config.sendEmail(to, bcc, subject, body, contentType, attachments...)
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
2023-01-15 14:28:31 +00:00
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
// ReloadProviderConf reloads the configuration from the provider
|
|
|
|
// and apply it if different from the active one
|
|
|
|
func ReloadProviderConf() {
|
|
|
|
loadConfigFromProvider() //nolint:errcheck
|
|
|
|
}
|
2023-01-15 14:28:31 +00:00
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
func loadConfigFromProvider() error {
|
|
|
|
configs, err := dataprovider.GetConfigs()
|
2023-01-15 14:28:31 +00:00
|
|
|
if err != nil {
|
2023-02-19 18:03:45 +00:00
|
|
|
logger.Error(logSender, "", "unable to load config from provider: %v", err)
|
|
|
|
return fmt.Errorf("smtp: unable to load config from provider: %w", err)
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
configs.SetNilsToEmpty()
|
2023-06-03 14:17:32 +00:00
|
|
|
if err := configs.SMTP.TryDecrypt(); err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to decrypt smtp config: %v", err)
|
|
|
|
return fmt.Errorf("smtp: unable to decrypt smtp config: %w", err)
|
2023-02-19 18:03:45 +00:00
|
|
|
}
|
|
|
|
config.Set(configs.SMTP)
|
|
|
|
return nil
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-06-03 14:17:32 +00:00
|
|
|
|
|
|
|
func updateRefreshToken(token string) {
|
|
|
|
configs, err := dataprovider.GetConfigs()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to load config from provider, updating refresh token not possible: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configs.SetNilsToEmpty()
|
|
|
|
if configs.SMTP.IsEmpty() {
|
|
|
|
logger.Warn(logSender, "", "unable to update refresh token, smtp not configured in the data provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configs.SMTP.OAuth2.RefreshToken = kms.NewPlainSecret(token)
|
|
|
|
if err := dataprovider.UpdateConfigs(&configs, dataprovider.ActionExecutorSystem, "", ""); err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to save new refresh token: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info(logSender, "", "refresh token updated")
|
|
|
|
}
|