Skip to content

Commit 38d11ee

Browse files
Fix send mail (#13312)
* Fix send mail * Fix send mail * Update modules/private/mail.go Co-authored-by: techknowlogick <[email protected]>
1 parent dbebc6b commit 38d11ee

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

cmd/mailer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import (
99
"net/http"
1010

1111
"code.gitea.io/gitea/modules/private"
12+
"code.gitea.io/gitea/modules/setting"
1213
"github.com/urfave/cli"
1314
)
1415

1516
func runSendMail(c *cli.Context) error {
17+
setting.NewContext()
18+
1619
if err := argsSet(c, "title"); err != nil {
1720
return err
1821
}
@@ -38,11 +41,11 @@ func runSendMail(c *cli.Context) error {
3841

3942
status, message := private.SendEmail(subject, body, nil)
4043
if status != http.StatusOK {
41-
fmt.Printf("error: %s", message)
44+
fmt.Printf("error: %s\n", message)
4245
return nil
4346
}
4447

45-
fmt.Printf("Succseded: %s", message)
48+
fmt.Printf("Success: %s\n", message)
4649

4750
return nil
4851
}

modules/private/mail.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ func SendEmail(subject, message string, to []string) (int, string) {
4949
return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error())
5050
}
5151

52-
return http.StatusOK, fmt.Sprintf("Was sent %s from %d", body, len(to))
52+
var users = fmt.Sprintf("%d", len(to))
53+
if len(to) == 0 {
54+
users = "all"
55+
}
56+
57+
return http.StatusOK, fmt.Sprintf("Sent %s email(s) to %s users", body, users)
5358
}

routers/private/mail.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,41 @@
55
package private
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"net/http"
1011
"strconv"
1112

1213
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/private"
16+
"code.gitea.io/gitea/modules/setting"
1517
"code.gitea.io/gitea/services/mailer"
1618
"gitea.com/macaron/macaron"
1719
)
1820

1921
// SendEmail pushes messages to mail queue
2022
//
2123
// It doesn't wait before each message will be processed
22-
func SendEmail(ctx *macaron.Context, mail private.Email) {
24+
func SendEmail(ctx *macaron.Context) {
25+
if setting.MailService == nil {
26+
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
27+
"err": "Mail service is not enabled.",
28+
})
29+
return
30+
}
31+
32+
var mail private.Email
33+
rd := ctx.Req.Body().ReadCloser()
34+
defer rd.Close()
35+
if err := json.NewDecoder(rd).Decode(&mail); err != nil {
36+
log.Error("%v", err)
37+
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
38+
"err": err,
39+
})
40+
return
41+
}
42+
2343
var emails []string
2444
if len(mail.To) > 0 {
2545
for _, uname := range mail.To {
@@ -33,13 +53,15 @@ func SendEmail(ctx *macaron.Context, mail private.Email) {
3353
return
3454
}
3555

36-
if user != nil {
56+
if user != nil && len(user.Email) > 0 {
3757
emails = append(emails, user.Email)
3858
}
3959
}
4060
} else {
4161
err := models.IterateUser(func(user *models.User) error {
42-
emails = append(emails, user.Email)
62+
if len(user.Email) > 0 {
63+
emails = append(emails, user.Email)
64+
}
4365
return nil
4466
})
4567
if err != nil {

0 commit comments

Comments
 (0)