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"
|
2021-09-26 18:25:37 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-02 20:25:41 +00:00
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
2021-09-26 18:25:37 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
mail "github.com/xhit/go-simple-mail/v2"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-10-02 20:25:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-09-26 18:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "smtp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EmailContentType defines the support content types for email body
|
|
|
|
type EmailContentType int
|
|
|
|
|
|
|
|
// Supporte email body content type
|
|
|
|
const (
|
|
|
|
EmailContentTypeTextPlain EmailContentType = iota
|
|
|
|
EmailContentTypeTextHTML
|
|
|
|
)
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
const (
|
|
|
|
templateEmailDir = "email"
|
|
|
|
templateRetentionCheckResult = "retention-check-report.html"
|
2021-11-13 12:25:43 +00:00
|
|
|
templatePasswordReset = "reset-password.html"
|
2021-10-02 20:25:41 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
var (
|
2021-10-02 20:25:41 +00:00
|
|
|
smtpServer *mail.SMTPServer
|
|
|
|
from string
|
|
|
|
emailTemplates = make(map[string]*template.Template)
|
2021-09-26 18:25:37 +00:00
|
|
|
)
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
// IsEnabled returns true if an SMTP server is configured
|
|
|
|
func IsEnabled() bool {
|
|
|
|
return smtpServer != nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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"`
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initialized and validates the SMTP configuration
|
2021-10-02 20:25:41 +00:00
|
|
|
func (c *Config) Initialize(configDir string) error {
|
2021-09-26 18:25:37 +00:00
|
|
|
smtpServer = nil
|
|
|
|
if c.Host == "" {
|
|
|
|
logger.Debug(logSender, "", "configuration disabled, email capabilities will not be available")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.Port <= 0 || c.Port > 65535 {
|
|
|
|
return fmt.Errorf("smtp: invalid port %v", c.Port)
|
|
|
|
}
|
|
|
|
if c.AuthType < 0 || c.AuthType > 2 {
|
|
|
|
return fmt.Errorf("smtp: invalid auth type %v", c.AuthType)
|
|
|
|
}
|
|
|
|
if c.Encryption < 0 || c.Encryption > 2 {
|
|
|
|
return fmt.Errorf("smtp: invalid encryption %v", c.Encryption)
|
|
|
|
}
|
2022-05-05 09:27:19 +00:00
|
|
|
templatesPath := util.FindSharedDataPath(c.TemplatesPath, configDir)
|
|
|
|
if templatesPath == "" {
|
2021-10-02 20:25:41 +00:00
|
|
|
return fmt.Errorf("smtp: invalid templates path %#v", templatesPath)
|
|
|
|
}
|
|
|
|
loadTemplates(filepath.Join(templatesPath, templateEmailDir))
|
2021-09-26 18:25:37 +00:00
|
|
|
from = c.From
|
|
|
|
smtpServer = mail.NewSMTPClient()
|
|
|
|
smtpServer.Host = c.Host
|
|
|
|
smtpServer.Port = c.Port
|
|
|
|
smtpServer.Username = c.User
|
|
|
|
smtpServer.Password = c.Password
|
|
|
|
smtpServer.Authentication = c.getAuthType()
|
|
|
|
smtpServer.Encryption = c.getEncryption()
|
|
|
|
smtpServer.KeepAlive = false
|
|
|
|
smtpServer.ConnectTimeout = 10 * time.Second
|
|
|
|
smtpServer.SendTimeout = 30 * time.Second
|
|
|
|
if c.Domain != "" {
|
|
|
|
smtpServer.Helo = c.Domain
|
|
|
|
}
|
|
|
|
logger.Debug(logSender, "", "configuration successfully initialized, host: %#v, port: %v, username: %#v, auth: %v, encryption: %v, helo: %#v",
|
|
|
|
smtpServer.Host, smtpServer.Port, smtpServer.Username, smtpServer.Authentication, smtpServer.Encryption, smtpServer.Helo)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) getEncryption() mail.Encryption {
|
|
|
|
switch c.Encryption {
|
|
|
|
case 1:
|
|
|
|
return mail.EncryptionSSLTLS
|
|
|
|
case 2:
|
|
|
|
return mail.EncryptionSTARTTLS
|
|
|
|
default:
|
|
|
|
return mail.EncryptionNone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) getAuthType() mail.AuthType {
|
|
|
|
if c.User == "" && c.Password == "" {
|
|
|
|
return mail.AuthNone
|
|
|
|
}
|
|
|
|
switch c.AuthType {
|
|
|
|
case 1:
|
|
|
|
return mail.AuthLogin
|
|
|
|
case 2:
|
|
|
|
return mail.AuthCRAMMD5
|
|
|
|
default:
|
|
|
|
return mail.AuthPlain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
func loadTemplates(templatesPath string) {
|
|
|
|
logger.Debug(logSender, "", "loading templates from %#v", templatesPath)
|
|
|
|
retentionCheckPath := filepath.Join(templatesPath, templateRetentionCheckResult)
|
|
|
|
retentionTmpl := util.LoadTemplate(nil, retentionCheckPath)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
passwordResetPath := filepath.Join(templatesPath, templatePasswordReset)
|
|
|
|
pwdResetTmpl := util.LoadTemplate(nil, passwordResetPath)
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
emailTemplates[templateRetentionCheckResult] = retentionTmpl
|
2021-11-13 12:25:43 +00:00
|
|
|
emailTemplates[templatePasswordReset] = pwdResetTmpl
|
2021-10-02 20:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderRetentionReportTemplate executes the retention report template
|
|
|
|
func RenderRetentionReportTemplate(buf *bytes.Buffer, data interface{}) error {
|
|
|
|
if smtpServer == nil {
|
|
|
|
return errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
return emailTemplates[templateRetentionCheckResult].Execute(buf, data)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// RenderPasswordResetTemplate executes the password reset template
|
2021-11-13 12:25:43 +00:00
|
|
|
func RenderPasswordResetTemplate(buf *bytes.Buffer, data interface{}) error {
|
|
|
|
if smtpServer == nil {
|
|
|
|
return errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
return emailTemplates[templatePasswordReset].Execute(buf, data)
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:25:37 +00:00
|
|
|
// SendEmail tries to send an email using the specified parameters.
|
|
|
|
func SendEmail(to, subject, body string, contentType EmailContentType) error {
|
|
|
|
if smtpServer == nil {
|
|
|
|
return errors.New("smtp: not configured")
|
|
|
|
}
|
|
|
|
smtpClient, err := smtpServer.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("smtp: unable to connect: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
email := mail.NewMSG()
|
|
|
|
if from != "" {
|
|
|
|
email.SetFrom(from)
|
2021-10-02 20:25:41 +00:00
|
|
|
} else {
|
|
|
|
email.SetFrom(smtpServer.Username)
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
|
|
|
email.AddTo(to).SetSubject(subject)
|
|
|
|
switch contentType {
|
|
|
|
case EmailContentTypeTextPlain:
|
|
|
|
email.SetBody(mail.TextPlain, body)
|
|
|
|
case EmailContentTypeTextHTML:
|
|
|
|
email.SetBody(mail.TextHTML, body)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("smtp: unsupported body content type %v", contentType)
|
|
|
|
}
|
|
|
|
if email.Error != nil {
|
|
|
|
return fmt.Errorf("smtp: email error: %w", email.Error)
|
|
|
|
}
|
|
|
|
return email.Send(smtpClient)
|
|
|
|
}
|