diff --git a/pkg/cmd/testmail.go b/pkg/cmd/testmail.go new file mode 100644 index 00000000..03a38449 --- /dev/null +++ b/pkg/cmd/testmail.go @@ -0,0 +1,42 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package cmd + +import ( + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/mail" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(testmailCmd) +} + +var testmailCmd = &cobra.Command{ + Use: "testmail [email]", + Short: "Send a test mail using the configured smtp connection", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + log.Info("Sending testmail...") + email := args[0] + if err := mail.SendTestMail(email); err != nil { + log.Errorf("Error sending test mail: %s", err.Error()) + return + } + log.Info("Testmail successfully sent.") + }, +} diff --git a/pkg/mail/mail.go b/pkg/mail/mail.go index 86748180..5caa9027 100644 --- a/pkg/mail/mail.go +++ b/pkg/mail/mail.go @@ -27,6 +27,16 @@ import ( // Queue is the mail queue var Queue chan *gomail.Message +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(), + } + return d +} + // StartMailDaemon starts the mail daemon func StartMailDaemon() { Queue = make(chan *gomail.Message, config.MailerQueuelength.GetInt()) @@ -41,9 +51,7 @@ func StartMailDaemon() { } go func() { - d := gomail.NewDialer(config.MailerHost.GetString(), config.MailerPort.GetInt(), config.MailerUsername.GetString(), config.MailerPassword.GetString()) - // #nosec - d.TLSConfig = &tls.Config{InsecureSkipVerify: config.MailerSkipTLSVerify.GetBool()} + d := getDialer() var s gomail.SendCloser var err error diff --git a/pkg/mail/send_mail.go b/pkg/mail/send_mail.go index 1901cbcb..536c97d1 100644 --- a/pkg/mail/send_mail.go +++ b/pkg/mail/send_mail.go @@ -53,6 +53,30 @@ type header struct { Content string } +// SendTestMail sends a test mail to a receipient. +// It works without a queue. +func SendTestMail(to string) error { + if config.MailerHost.GetString() == "" { + log.Warning("Mailer seems to be not configured! Please see the config docs for more details.") + return nil + } + + d := getDialer() + s, err := d.Dial() + if err != nil { + return err + } + defer s.Close() + + m := gomail.NewMessage() + m.SetHeader("From", config.MailerFromEmail.GetString()) + m.SetHeader("To", to) + m.SetHeader("Subject", "Test from Vikunja") + m.SetBody("text/plain", "This is a test mail! If you got this, Vikunja is correctly set up to send emails.") + + return gomail.Send(s, m) +} + // SendMail puts a mail in the queue func SendMail(opts *Opts) { m := gomail.NewMessage()