Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix send mail #13312

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
7 changes: 6 additions & 1 deletion modules/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("Was sent %s from %s", body, users)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
}
28 changes: 25 additions & 3 deletions routers/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@
package private

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"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"
)

// 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 {
Expand All @@ -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 {
Expand Down