Skip to content

Commit 6e232b4

Browse files
committed
Protect public functions within the mailer by testing if the mailer is configured
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 79e29e8 commit 6e232b4

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

services/mailer/mail.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) {
5757

5858
// SendTestMail sends a test mail
5959
func SendTestMail(email string) error {
60+
if setting.MailService == nil {
61+
// No mail service configured
62+
return nil
63+
}
6064
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
6165
}
6266

@@ -90,17 +94,29 @@ func sendUserMail(language string, u *models.User, tpl base.TplName, code, subje
9094

9195
// SendActivateAccountMail sends an activation mail to the user (new user registration)
9296
func SendActivateAccountMail(locale translation.Locale, u *models.User) {
97+
if setting.MailService == nil {
98+
// No mail service configured
99+
return
100+
}
93101
sendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateEmailActivateCode(u.Email), locale.Tr("mail.activate_account"), "activate account")
94102
}
95103

96104
// SendResetPasswordMail sends a password reset mail to the user
97105
func SendResetPasswordMail(u *models.User) {
106+
if setting.MailService == nil {
107+
// No mail service configured
108+
return
109+
}
98110
locale := translation.NewLocale(u.Language)
99111
sendUserMail(u.Language, u, mailAuthResetPassword, u.GenerateEmailActivateCode(u.Email), locale.Tr("mail.reset_password"), "recover account")
100112
}
101113

102114
// SendActivateEmailMail sends confirmation email to confirm new email address
103115
func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {
116+
if setting.MailService == nil {
117+
// No mail service configured
118+
return
119+
}
104120
locale := translation.NewLocale(u.Language)
105121
data := map[string]interface{}{
106122
"DisplayName": u.DisplayName(),
@@ -129,6 +145,10 @@ func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {
129145

130146
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
131147
func SendRegisterNotifyMail(u *models.User) {
148+
if setting.MailService == nil {
149+
// No mail service configured
150+
return
151+
}
132152
locale := translation.NewLocale(u.Language)
133153

134154
data := map[string]interface{}{
@@ -156,6 +176,10 @@ func SendRegisterNotifyMail(u *models.User) {
156176

157177
// SendCollaboratorMail sends mail notification to new collaborator.
158178
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
179+
if setting.MailService == nil {
180+
// No mail service configured
181+
return
182+
}
159183
locale := translation.NewLocale(u.Language)
160184
repoName := repo.FullName()
161185

@@ -344,6 +368,10 @@ func sanitizeSubject(subject string) string {
344368

345369
// SendIssueAssignedMail composes and sends issue assigned email
346370
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) error {
371+
if setting.MailService == nil {
372+
// No mail service configured
373+
return nil
374+
}
347375
langMap := make(map[string][]*models.User)
348376
for _, user := range recipients {
349377
langMap[user.Language] = append(langMap[user.Language], user)

services/mailer/mail_comment.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ package mailer
77
import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/setting"
1011
)
1112

1213
// MailParticipantsComment sends new comment emails to repository watchers and mentioned people.
1314
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) error {
15+
if setting.MailService == nil {
16+
// No mail service configured
17+
return nil
18+
}
19+
1420
content := c.Content
1521
if c.Type == models.CommentTypePullPush {
1622
content = ""
@@ -30,6 +36,11 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue
3036

3137
// MailMentionsComment sends email to users mentioned in a code comment
3238
func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) (err error) {
39+
if setting.MailService == nil {
40+
// No mail service configured
41+
return nil
42+
}
43+
3344
visited := make(map[int64]bool, len(mentions)+1)
3445
visited[c.Poster.ID] = true
3546
if err = mailIssueCommentBatch(

services/mailer/mail_issue.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/setting"
1213
)
1314

1415
func fallbackMailSubject(issue *models.Issue) string {
@@ -163,6 +164,11 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visite
163164
// MailParticipants sends new issue thread created emails to repository watchers
164165
// and mentioned people.
165166
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error {
167+
if setting.MailService == nil {
168+
// No mail service configured
169+
return nil
170+
}
171+
166172
content := issue.Content
167173
if opType == models.ActionCloseIssue || opType == models.ActionClosePullRequest ||
168174
opType == models.ActionReopenIssue || opType == models.ActionReopenPullRequest ||

services/mailer/mail_release.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const (
2323

2424
// MailNewRelease send new release notify to all all repo watchers.
2525
func MailNewRelease(rel *models.Release) {
26+
if setting.MailService == nil {
27+
// No mail service configured
28+
return
29+
}
30+
2631
watcherIDList, err := models.GetRepoWatchersIDs(rel.RepoID)
2732
if err != nil {
2833
log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err)

services/mailer/mail_repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import (
99
"fmt"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/setting"
1213
"code.gitea.io/gitea/modules/templates"
1314
"code.gitea.io/gitea/modules/translation"
1415
)
1516

1617
// SendRepoTransferNotifyMail triggers a notification e-mail when a pending repository transfer was created
1718
func SendRepoTransferNotifyMail(doer, newOwner *models.User, repo *models.Repository) error {
19+
if setting.MailService == nil {
20+
// No mail service configured
21+
return nil
22+
}
23+
1824
if newOwner.IsOrganization() {
1925
users, err := models.GetUsersWhoCanCreateOrgRepo(newOwner.ID)
2026
if err != nil {

0 commit comments

Comments
 (0)