2020-02-07 16:27:45 +00:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2023-09-01 06:32:28 +00:00
|
|
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
2018-11-26 20:17:33 +00:00
|
|
|
//
|
2019-12-04 19:39:56 +00:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 15:41:52 +00:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 19:39:56 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 20:17:33 +00:00
|
|
|
//
|
2019-12-04 19:39:56 +00:00
|
|
|
// 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
|
2020-12-23 15:41:52 +00:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 20:17:33 +00:00
|
|
|
//
|
2020-12-23 15:41:52 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 19:39:56 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 20:17:33 +00:00
|
|
|
|
2018-10-27 09:33:28 +00:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
2022-06-19 13:57:00 +00:00
|
|
|
"context"
|
2022-07-11 14:10:28 +00:00
|
|
|
"crypto/tls"
|
2020-10-11 20:10:03 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-06 20:12:26 +00:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
2018-10-31 12:42:38 +00:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2022-06-19 13:57:00 +00:00
|
|
|
|
|
|
|
"github.com/wneessen/go-mail"
|
2018-10-27 09:33:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Queue is the mail queue
|
2022-06-19 13:57:00 +00:00
|
|
|
var Queue chan *mail.Msg
|
|
|
|
|
|
|
|
func getClient() (*mail.Client, error) {
|
2018-10-27 09:33:28 +00:00
|
|
|
|
2022-06-19 13:57:00 +00:00
|
|
|
var authType mail.SMTPAuthType
|
|
|
|
switch config.MailerAuthType.GetString() {
|
|
|
|
case "plain":
|
|
|
|
authType = mail.SMTPAuthPlain
|
|
|
|
case "login":
|
|
|
|
authType = mail.SMTPAuthLogin
|
|
|
|
case "cram-md5":
|
|
|
|
authType = mail.SMTPAuthCramMD5
|
2020-05-29 13:10:06 +00:00
|
|
|
}
|
2022-06-19 13:57:00 +00:00
|
|
|
|
|
|
|
tlsPolicy := mail.TLSOpportunistic
|
|
|
|
if config.MailerForceSSL.GetBool() {
|
|
|
|
tlsPolicy = mail.TLSMandatory
|
|
|
|
}
|
|
|
|
|
2022-07-12 13:46:21 +00:00
|
|
|
opts := []mail.Option{
|
2022-06-19 13:57:00 +00:00
|
|
|
mail.WithPort(config.MailerPort.GetInt()),
|
2024-06-18 11:28:10 +00:00
|
|
|
mail.WithTLSPortPolicy(tlsPolicy),
|
2022-07-11 14:10:28 +00:00
|
|
|
mail.WithTLSConfig(&tls.Config{
|
2024-03-10 13:47:19 +00:00
|
|
|
//#nosec G402
|
2022-07-11 14:10:28 +00:00
|
|
|
InsecureSkipVerify: config.MailerSkipTLSVerify.GetBool(),
|
2022-07-13 17:57:44 +00:00
|
|
|
ServerName: config.MailerHost.GetString(),
|
2022-07-11 14:10:28 +00:00
|
|
|
}),
|
2022-09-26 16:09:39 +00:00
|
|
|
mail.WithTimeout((config.MailerQueueTimeout.GetDuration() + 3) * time.Second), // 3s more for us to close before mail server timeout
|
2023-10-03 09:28:28 +00:00
|
|
|
mail.WithLogger(log.NewMailLogger(config.LogEnabled.GetBool(), config.LogMail.GetString(), config.LogMailLevel.GetString())),
|
2023-03-24 19:35:12 +00:00
|
|
|
mail.WithDebugLog(),
|
2022-07-12 13:46:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 11:39:43 +00:00
|
|
|
if config.MailerForceSSL.GetBool() {
|
2024-06-18 11:28:10 +00:00
|
|
|
opts = append(opts, mail.WithSSLPort(true))
|
2023-01-09 11:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 11:41:42 +00:00
|
|
|
if config.MailerUsername.GetString() != "" && config.MailerPassword.GetString() != "" {
|
|
|
|
opts = append(opts, mail.WithSMTPAuth(authType))
|
|
|
|
}
|
|
|
|
|
2022-07-12 13:46:21 +00:00
|
|
|
if config.MailerUsername.GetString() != "" {
|
|
|
|
opts = append(opts, mail.WithUsername(config.MailerUsername.GetString()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.MailerPassword.GetString() != "" {
|
|
|
|
opts = append(opts, mail.WithPassword(config.MailerPassword.GetString()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return mail.NewClient(
|
|
|
|
config.MailerHost.GetString(),
|
|
|
|
opts...,
|
2022-07-11 14:10:28 +00:00
|
|
|
)
|
2020-05-29 13:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 09:33:28 +00:00
|
|
|
// StartMailDaemon starts the mail daemon
|
|
|
|
func StartMailDaemon() {
|
2022-06-19 13:57:00 +00:00
|
|
|
Queue = make(chan *mail.Msg, config.MailerQueuelength.GetInt())
|
2018-10-28 16:11:13 +00:00
|
|
|
|
2019-07-06 20:12:26 +00:00
|
|
|
if !config.MailerEnabled.GetBool() {
|
2018-12-19 21:05:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:12:26 +00:00
|
|
|
if config.MailerHost.GetString() == "" {
|
2019-07-20 18:12:10 +00:00
|
|
|
log.Warning("Mailer seems to be not configured! Please see the config docs for more details.")
|
2018-10-27 09:33:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-19 13:57:00 +00:00
|
|
|
c, err := getClient()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not create mail client: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2018-10-27 09:33:28 +00:00
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
open := false
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-Queue:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !open {
|
2022-06-19 13:57:00 +00:00
|
|
|
err = c.DialWithContext(context.Background())
|
|
|
|
if err != nil {
|
2022-09-01 12:19:00 +00:00
|
|
|
log.Errorf("Error during connect to smtp server: %s", err)
|
2021-08-01 22:05:13 +00:00
|
|
|
break
|
2018-10-27 09:33:28 +00:00
|
|
|
}
|
|
|
|
open = true
|
|
|
|
}
|
2022-06-19 13:57:00 +00:00
|
|
|
err = c.Send(m)
|
|
|
|
if err != nil {
|
2022-09-01 12:19:00 +00:00
|
|
|
log.Errorf("Error when sending mail: %s", err)
|
2021-08-01 22:05:13 +00:00
|
|
|
break
|
2018-10-27 09:33:28 +00:00
|
|
|
}
|
|
|
|
// Close the connection to the SMTP server if no email was sent in
|
|
|
|
// the last 30 seconds.
|
2019-07-06 20:12:26 +00:00
|
|
|
case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second):
|
2018-10-27 09:33:28 +00:00
|
|
|
if open {
|
2021-08-01 22:05:13 +00:00
|
|
|
open = false
|
2022-06-19 13:57:00 +00:00
|
|
|
err = c.Close()
|
|
|
|
if err != nil {
|
2022-09-01 12:19:00 +00:00
|
|
|
log.Errorf("Error closing the mail server connection: %s\n", err)
|
2021-08-01 22:05:13 +00:00
|
|
|
break
|
2018-10-27 09:33:28 +00:00
|
|
|
}
|
2022-09-01 12:19:00 +00:00
|
|
|
log.Info("Closed connection to mail server")
|
2018-10-27 09:33:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|