2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01: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 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-10-27 11:33:28 +02:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"time"
|
|
|
|
|
2019-07-06 22:12:26 +02:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
2018-10-31 13:42:38 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2018-10-27 11:33:28 +02:00
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Queue is the mail queue
|
|
|
|
var Queue chan *gomail.Message
|
|
|
|
|
2020-05-29 15:10:06 +02:00
|
|
|
func getDialer() *gomail.Dialer {
|
|
|
|
d := gomail.NewDialer(config.MailerHost.GetString(), config.MailerPort.GetInt(), config.MailerUsername.GetString(), config.MailerPassword.GetString())
|
|
|
|
// #nosec
|
|
|
|
d.TLSConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: config.MailerSkipTLSVerify.GetBool(),
|
|
|
|
ServerName: config.MailerHost.GetString(),
|
|
|
|
}
|
2020-07-14 17:30:39 +02:00
|
|
|
d.SSL = config.MailerForceSSL.GetBool()
|
2020-05-29 15:10:06 +02:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-10-27 11:33:28 +02:00
|
|
|
// StartMailDaemon starts the mail daemon
|
|
|
|
func StartMailDaemon() {
|
2019-07-06 22:12:26 +02:00
|
|
|
Queue = make(chan *gomail.Message, config.MailerQueuelength.GetInt())
|
2018-10-28 17:11:13 +01:00
|
|
|
|
2019-07-06 22:12:26 +02:00
|
|
|
if !config.MailerEnabled.GetBool() {
|
2018-12-19 22:05:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-06 22:12:26 +02:00
|
|
|
if config.MailerHost.GetString() == "" {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Warning("Mailer seems to be not configured! Please see the config docs for more details.")
|
2018-10-27 11:33:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-05-29 15:10:06 +02:00
|
|
|
d := getDialer()
|
2018-10-27 11:33:28 +02:00
|
|
|
|
|
|
|
var s gomail.SendCloser
|
|
|
|
var err error
|
|
|
|
open := false
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-Queue:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !open {
|
|
|
|
if s, err = d.Dial(); err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error("Error during connect to smtp server: %s", err)
|
2018-10-27 11:33:28 +02:00
|
|
|
}
|
|
|
|
open = true
|
|
|
|
}
|
|
|
|
if err := gomail.Send(s, m); err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error("Error when sending mail: %s", err)
|
2018-10-27 11:33:28 +02:00
|
|
|
}
|
|
|
|
// Close the connection to the SMTP server if no email was sent in
|
|
|
|
// the last 30 seconds.
|
2019-07-06 22:12:26 +02:00
|
|
|
case <-time.After(config.MailerQueueTimeout.GetDuration() * time.Second):
|
2018-10-27 11:33:28 +02:00
|
|
|
if open {
|
|
|
|
if err := s.Close(); err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error("Error closing the mail server connection: %s\n", err)
|
2018-10-27 11:33:28 +02:00
|
|
|
}
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Infof("Closed connection to mailserver")
|
2018-10-27 11:33:28 +02:00
|
|
|
open = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopMailDaemon closes the mail queue channel, aka stops the daemon
|
|
|
|
func StopMailDaemon() {
|
|
|
|
close(Queue)
|
|
|
|
}
|