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

Prevent multiple To recipients #22566

Merged
merged 4 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion routers/private/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func SendEmail(ctx *context.PrivateContext) {

func sendEmail(ctx *context.PrivateContext, subject, message string, to []string) {
for _, email := range to {
msg := mailer.NewMessage([]string{email}, subject, message)
msg := mailer.NewMessage(email, subject, message)
mailer.SendAsync(msg)
}

Expand Down
12 changes: 6 additions & 6 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func SendTestMail(email string) error {
// No mail service configured
return nil
}
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
return gomail.Send(Sender, NewMessage(email, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
}

// sendUserMail sends a mail to the user
Expand All @@ -86,7 +86,7 @@ func sendUserMail(language string, u *user_model.User, tpl base.TplName, code, s
return
}

msg := NewMessage([]string{u.Email}, subject, content.String())
msg := NewMessage(u.Email, subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)

SendAsync(msg)
Expand Down Expand Up @@ -137,7 +137,7 @@ func SendActivateEmailMail(u *user_model.User, email *user_model.EmailAddress) {
return
}

msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
msg := NewMessage(email.Email, locale.Tr("mail.activate_email"), content.String())
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)

SendAsync(msg)
Expand Down Expand Up @@ -168,7 +168,7 @@ func SendRegisterNotifyMail(u *user_model.User) {
return
}

msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
msg := NewMessage(u.Email, locale.Tr("mail.register_notify"), content.String())
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)

SendAsync(msg)
Expand Down Expand Up @@ -202,7 +202,7 @@ func SendCollaboratorMail(u, doer *user_model.User, repo *repo_model.Repository)
return
}

msg := NewMessage([]string{u.Email}, subject, content.String())
msg := NewMessage(u.Email, subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)

SendAsync(msg)
Expand Down Expand Up @@ -322,7 +322,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient

msgs := make([]*Message, 0, len(recipients))
for _, recipient := range recipients {
msg := NewMessageFrom([]string{recipient.Email}, ctx.Doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
msg := NewMessageFrom(recipient.Email, ctx.Doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)

msg.SetHeader("Message-ID", msgID)
Expand Down
2 changes: 1 addition & 1 deletion services/mailer/mail_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func mailNewRelease(ctx context.Context, lang string, tos []string, rel *repo_mo
publisherName := rel.Publisher.DisplayName()
relURL := "<" + rel.HTMLURL() + ">"
for _, to := range tos {
msg := NewMessageFrom([]string{to}, publisherName, setting.MailService.FromEmail, subject, mailBody.String())
msg := NewMessageFrom(to, publisherName, setting.MailService.FromEmail, subject, mailBody.String())
msg.Info = subject
msg.SetHeader("Message-ID", relURL)
msgs = append(msgs, msg)
Expand Down
9 changes: 6 additions & 3 deletions services/mailer/mail_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ func sendRepoTransferNotifyMailPerLang(lang string, newOwner, doer *user_model.U
return err
}

msg := NewMessage(emails, subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, repository pending transfer notification", newOwner.ID)
for _, to := range emails {
msg := NewMessage(to, subject, content.String())
msg.Info = fmt.Sprintf("UID: %d, repository pending transfer notification", newOwner.ID)

SendAsync(msg)
}

SendAsync(msg)
return nil
}
2 changes: 1 addition & 1 deletion services/mailer/mail_team_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_mod
return err
}

msg := NewMessage([]string{invite.Email}, subject, mailBody.String())
msg := NewMessage(invite.Email, subject, mailBody.String())
msg.Info = subject

SendAsync(msg)
Expand Down
10 changes: 5 additions & 5 deletions services/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Message struct {
Info string // Message information for log purpose.
FromAddress string
FromDisplayName string
To []string
To string
ReplyTo string
Subject string
Date time.Time
Expand All @@ -47,7 +47,7 @@ type Message struct {
func (m *Message) ToMessage() *gomail.Message {
msg := gomail.NewMessage()
msg.SetAddressHeader("From", m.FromAddress, m.FromDisplayName)
msg.SetHeader("To", m.To...)
msg.SetHeader("To", m.To)
if m.ReplyTo != "" {
msg.SetHeader("Reply-To", m.ReplyTo)
}
Expand Down Expand Up @@ -89,15 +89,15 @@ func (m *Message) generateAutoMessageID() string {
dateMs := m.Date.UnixNano() / 1e6
h := fnv.New64()
if len(m.To) > 0 {
_, _ = h.Write([]byte(m.To[0]))
_, _ = h.Write([]byte(m.To))
}
_, _ = h.Write([]byte(m.Subject))
_, _ = h.Write([]byte(m.Body))
return fmt.Sprintf("<autogen-%d-%016x@%s>", dateMs, h.Sum64(), setting.Domain)
}

// NewMessageFrom creates new mail message object with custom From header.
func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message {
func NewMessageFrom(to string, fromDisplayName, fromAddress, subject, body string) *Message {
delvh marked this conversation as resolved.
Show resolved Hide resolved
log.Trace("NewMessageFrom (body):\n%s", body)

return &Message{
Expand All @@ -112,7 +112,7 @@ func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body str
}

// NewMessage creates new mail message object with default From header.
func NewMessage(to []string, subject, body string) *Message {
func NewMessage(to string, subject, body string) *Message {
delvh marked this conversation as resolved.
Show resolved Hide resolved
return NewMessageFrom(to, setting.MailService.FromName, setting.MailService.FromEmail, subject, body)
}

Expand Down
6 changes: 3 additions & 3 deletions services/mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func TestGenerateMessageID(t *testing.T) {
setting.Domain = "localhost"

date := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
m := NewMessageFrom(nil, "display-name", "from-address", "subject", "body")
m := NewMessageFrom("", "display-name", "from-address", "subject", "body")
m.Date = date
gm := m.ToMessage()
assert.Equal(t, "<autogen-946782245000-41e8fc54a8ad3a3f@localhost>", gm.GetHeader("Message-ID")[0])

m = NewMessageFrom([]string{"a@b.com"}, "display-name", "from-address", "subject", "body")
m = NewMessageFrom("a@b.com", "display-name", "from-address", "subject", "body")
m.Date = date
gm = m.ToMessage()
assert.Equal(t, "<autogen-946782245000-cc88ce3cfe9bd04f@localhost>", gm.GetHeader("Message-ID")[0])

m = NewMessageFrom([]string{"a@b.com"}, "display-name", "from-address", "subject", "body")
m = NewMessageFrom("a@b.com", "display-name", "from-address", "subject", "body")
m.SetHeader("Message-ID", "<msg-d@domain.com>")
gm = m.ToMessage()
assert.Equal(t, "<msg-d@domain.com>", gm.GetHeader("Message-ID")[0])
Expand Down