From c685250c96bdeff7512d4ebf1e3cdbdd78629dfd Mon Sep 17 00:00:00 2001 From: konrad Date: Fri, 29 May 2020 13:10:06 +0000 Subject: [PATCH] Add test mail command (#571) Add test mail command Co-authored-by: kolaente Reviewed-on: https://kolaente.dev/vikunja/api/pulls/571 --- pkg/cmd/testmail.go | 42 ++++++++++++++++++++++++++++++++++++++++++ pkg/mail/mail.go | 14 +++++++++++--- pkg/mail/send_mail.go | 24 ++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 pkg/cmd/testmail.go diff --git a/pkg/cmd/testmail.go b/pkg/cmd/testmail.go new file mode 100644 index 0000000000..03a384493f --- /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 86748180e4..5caa90273a 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 1901cbcb66..536c97d13f 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()