From de7b137257395ed4c5d3856988b554a1644227e4 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 27 Aug 2023 21:26:30 +0800 Subject: [PATCH] add send test email utility --- cmd/utility.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cmd/utility.go b/cmd/utility.go index 9c6a8afb..feca2faf 100644 --- a/cmd/utility.go +++ b/cmd/utility.go @@ -7,6 +7,8 @@ import ( "github.com/urfave/cli/v2" + "github.com/mayswind/ezbookkeeping/pkg/errs" + "github.com/mayswind/ezbookkeeping/pkg/mail" "github.com/mayswind/ezbookkeeping/pkg/requestid" "github.com/mayswind/ezbookkeeping/pkg/utils" ) @@ -28,6 +30,18 @@ var Utilities = &cli.Command{ }, }, }, + { + Name: "send-test-mail", + Usage: "Send an email to specified e-mail address", + Action: sendTestMail, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "to", + Required: true, + Usage: "To e-mail address", + }, + }, + }, }, } @@ -59,6 +73,34 @@ func parseRequestId(c *cli.Context) error { return nil } +func sendTestMail(c *cli.Context) error { + config, err := initializeSystem(c) + + if err != nil { + return err + } + + if !config.EnableSmtp || mail.Container.Current == nil { + return errs.ErrSmtpServerNotEnabled + } + + toAddress := c.String("to") + + err = mail.Container.Current.SendMail(&mail.MailMessage{ + To: toAddress, + Subject: "ezBookkeeping test e-mail", + Body: "This is a test e-mail", + }) + + if err != nil { + return err + } + + fmt.Printf("Test e-mail has been sent") + + return nil +} + func printRequestIdInfo(requestId string, requestIdInfo *requestid.RequestIdInfo, newRequestIdInfo *requestid.RequestIdInfo) { fmt.Printf("[RequestId] %s\n", requestId) fmt.Printf("[ServerUniqId] %d (Current Server %d)\n", requestIdInfo.ServerUniqId, newRequestIdInfo.ServerUniqId)