From 38d11eea58c6618fbab1a83e1637201964100e9a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 27 Oct 2020 00:42:27 +0800 Subject: [PATCH] Fix send mail (#13312) * Fix send mail * Fix send mail * Update modules/private/mail.go Co-authored-by: techknowlogick --- cmd/mailer.go | 7 +++++-- modules/private/mail.go | 7 ++++++- routers/private/mail.go | 28 +++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/cmd/mailer.go b/cmd/mailer.go index a9a9048a5e..ee11b56cc7 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -9,10 +9,13 @@ import ( "net/http" "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" "github.com/urfave/cli" ) func runSendMail(c *cli.Context) error { + setting.NewContext() + if err := argsSet(c, "title"); err != nil { return err } @@ -38,11 +41,11 @@ func runSendMail(c *cli.Context) error { status, message := private.SendEmail(subject, body, nil) if status != http.StatusOK { - fmt.Printf("error: %s", message) + fmt.Printf("error: %s\n", message) return nil } - fmt.Printf("Succseded: %s", message) + fmt.Printf("Success: %s\n", message) return nil } diff --git a/modules/private/mail.go b/modules/private/mail.go index db56009bb9..675dec8f11 100644 --- a/modules/private/mail.go +++ b/modules/private/mail.go @@ -49,5 +49,10 @@ func SendEmail(subject, message string, to []string) (int, string) { return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error()) } - return http.StatusOK, fmt.Sprintf("Was sent %s from %d", body, len(to)) + var users = fmt.Sprintf("%d", len(to)) + if len(to) == 0 { + users = "all" + } + + return http.StatusOK, fmt.Sprintf("Sent %s email(s) to %s users", body, users) } diff --git a/routers/private/mail.go b/routers/private/mail.go index 8d09752487..b3b21d042f 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -5,6 +5,7 @@ package private import ( + "encoding/json" "fmt" "net/http" "strconv" @@ -12,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/mailer" "gitea.com/macaron/macaron" ) @@ -19,7 +21,25 @@ import ( // SendEmail pushes messages to mail queue // // It doesn't wait before each message will be processed -func SendEmail(ctx *macaron.Context, mail private.Email) { +func SendEmail(ctx *macaron.Context) { + if setting.MailService == nil { + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": "Mail service is not enabled.", + }) + return + } + + var mail private.Email + rd := ctx.Req.Body().ReadCloser() + defer rd.Close() + if err := json.NewDecoder(rd).Decode(&mail); err != nil { + log.Error("%v", err) + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": err, + }) + return + } + var emails []string if len(mail.To) > 0 { for _, uname := range mail.To { @@ -33,13 +53,15 @@ func SendEmail(ctx *macaron.Context, mail private.Email) { return } - if user != nil { + if user != nil && len(user.Email) > 0 { emails = append(emails, user.Email) } } } else { err := models.IterateUser(func(user *models.User) error { - emails = append(emails, user.Email) + if len(user.Email) > 0 { + emails = append(emails, user.Email) + } return nil }) if err != nil {