2020-10-25 02:08:14 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-10-25 02:08:14 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/private"
|
2020-10-26 22:12:27 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-10-25 02:08:14 +05:30
|
|
|
"code.gitea.io/gitea/services/mailer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SendEmail pushes messages to mail queue
|
|
|
|
//
|
|
|
|
// It doesn't wait before each message will be processed
|
2021-01-26 21:06:53 +05:30
|
|
|
func SendEmail(ctx *context.PrivateContext) {
|
2020-10-26 22:12:27 +05:30
|
|
|
if setting.MailService == nil {
|
2021-06-24 01:08:19 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: "Mail service is not enabled.",
|
2020-10-26 22:12:27 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var mail private.Email
|
2021-01-26 21:06:53 +05:30
|
|
|
rd := ctx.Req.Body
|
2020-10-26 22:12:27 +05:30
|
|
|
defer rd.Close()
|
2021-07-24 21:33:58 +05:30
|
|
|
|
2020-10-26 22:12:27 +05:30
|
|
|
if err := json.NewDecoder(rd).Decode(&mail); err != nil {
|
|
|
|
log.Error("%v", err)
|
2021-06-24 01:08:19 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: err.Error(),
|
2020-10-26 22:12:27 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:08:14 +05:30
|
|
|
var emails []string
|
|
|
|
if len(mail.To) > 0 {
|
|
|
|
for _, uname := range mail.To {
|
2022-05-20 19:38:52 +05:30
|
|
|
user, err := user_model.GetUserByName(ctx, uname)
|
2020-10-25 02:08:14 +05:30
|
|
|
if err != nil {
|
|
|
|
err := fmt.Sprintf("Failed to get user information: %v", err)
|
|
|
|
log.Error(err)
|
2021-06-24 01:08:19 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: err,
|
2020-10-25 02:08:14 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-26 22:12:27 +05:30
|
|
|
if user != nil && len(user.Email) > 0 {
|
2020-10-25 02:08:14 +05:30
|
|
|
emails = append(emails, user.Email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-24 15:19:20 +05:30
|
|
|
err := user_model.IterateUser(func(user *user_model.User) error {
|
2022-03-19 18:15:44 +05:30
|
|
|
if len(user.Email) > 0 && user.IsActive {
|
2020-10-26 22:12:27 +05:30
|
|
|
emails = append(emails, user.Email)
|
|
|
|
}
|
2020-10-25 02:08:14 +05:30
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Sprintf("Failed to find users: %v", err)
|
|
|
|
log.Error(err)
|
2021-06-24 01:08:19 +05:30
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: err,
|
2020-10-25 02:08:14 +05:30
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendEmail(ctx, mail.Subject, mail.Message, emails)
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
func sendEmail(ctx *context.PrivateContext, subject, message string, to []string) {
|
2020-10-25 02:08:14 +05:30
|
|
|
for _, email := range to {
|
|
|
|
msg := mailer.NewMessage([]string{email}, subject, message)
|
|
|
|
mailer.SendAsync(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wasSent := strconv.Itoa(len(to))
|
|
|
|
|
2021-12-15 12:29:57 +05:30
|
|
|
ctx.PlainText(http.StatusOK, wasSent)
|
2020-10-25 02:08:14 +05:30
|
|
|
}
|