Browse Source

Fix send mail (#13312)

* Fix send mail

* Fix send mail

* Update modules/private/mail.go

Co-authored-by: techknowlogick <matti@mdranta.net>
tags/v1.15.0-dev
Lunny Xiao 3 years ago
parent
commit
38d11eea58
No account linked to committer's email address
3 changed files with 36 additions and 6 deletions
  1. 5
    2
      cmd/mailer.go
  2. 6
    1
      modules/private/mail.go
  3. 25
    3
      routers/private/mail.go

+ 5
- 2
cmd/mailer.go View File

"net/http" "net/http"


"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting"
"github.com/urfave/cli" "github.com/urfave/cli"
) )


func runSendMail(c *cli.Context) error { func runSendMail(c *cli.Context) error {
setting.NewContext()

if err := argsSet(c, "title"); err != nil { if err := argsSet(c, "title"); err != nil {
return err return err
} }


status, message := private.SendEmail(subject, body, nil) status, message := private.SendEmail(subject, body, nil)
if status != http.StatusOK { if status != http.StatusOK {
fmt.Printf("error: %s", message)
fmt.Printf("error: %s\n", message)
return nil return nil
} }


fmt.Printf("Succseded: %s", message)
fmt.Printf("Success: %s\n", message)


return nil return nil
} }

+ 6
- 1
modules/private/mail.go View File

return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error()) 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)
} }

+ 25
- 3
routers/private/mail.go View File

package private package private


import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/mailer" "code.gitea.io/gitea/services/mailer"
"gitea.com/macaron/macaron" "gitea.com/macaron/macaron"
) )
// SendEmail pushes messages to mail queue // SendEmail pushes messages to mail queue
// //
// It doesn't wait before each message will be processed // 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 var emails []string
if len(mail.To) > 0 { if len(mail.To) > 0 {
for _, uname := range mail.To { for _, uname := range mail.To {
return return
} }


if user != nil {
if user != nil && len(user.Email) > 0 {
emails = append(emails, user.Email) emails = append(emails, user.Email)
} }
} }
} else { } else {
err := models.IterateUser(func(user *models.User) error { 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 return nil
}) })
if err != nil { if err != nil {

Loading…
Cancel
Save