Refactor testmail command to use a notification

This commit is contained in:
kolaente 2021-02-07 16:37:11 +01:00
parent f64dba3615
commit 9d0bc9adf0
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 24 additions and 10 deletions

View File

@ -17,9 +17,11 @@
package cmd
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/initialize"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/mail"
"code.vikunja.io/api/pkg/notifications"
"github.com/spf13/cobra"
)
@ -39,8 +41,20 @@ var testmailCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
log.Info("Sending testmail...")
email := args[0]
if err := mail.SendTestMail(email); err != nil {
message := notifications.NewMail().
From(config.MailerFromEmail.GetString()).
To(args[0]).
Subject("Test from Vikunja").
Line("This is a test mail!").
Line("If you received this, Vikunja is correctly set up to send emails.").
Action("Go to your instance", config.ServiceFrontendurl.GetString())
opts, err := notifications.RenderMail(message)
if err != nil {
log.Errorf("Error sending test mail: %s", err.Error())
return
}
if err := mail.SendTestMail(opts); err != nil {
log.Errorf("Error sending test mail: %s", err.Error())
return
}

View File

@ -57,7 +57,7 @@ type header struct {
// SendTestMail sends a test mail to a receipient.
// It works without a queue.
func SendTestMail(to string) error {
func SendTestMail(opts *Opts) error {
if config.MailerHost.GetString() == "" {
log.Warning("Mailer seems to be not configured! Please see the config docs for more details.")
return nil
@ -70,17 +70,12 @@ func SendTestMail(to string) error {
}
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.")
m := sendMail(opts)
return gomail.Send(s, m)
}
// SendMail puts a mail in the queue
func SendMail(opts *Opts) {
func sendMail(opts *Opts) *gomail.Message {
m := gomail.NewMessage()
if opts.From == "" {
opts.From = config.MailerFromEmail.GetString()
@ -101,7 +96,12 @@ func SendMail(opts *Opts) {
m.SetBody("text/plain", opts.Message)
m.AddAlternative("text/html", opts.HTMLMessage)
}
return m
}
// SendMail puts a mail in the queue
func SendMail(opts *Opts) {
m := sendMail(opts)
Queue <- m
}