2022-10-19 08:40:28 -04:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-19 08:40:28 -04:00
|
|
|
|
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
org_model "code.gitea.io/gitea/models/organization"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
|
|
|
"code.gitea.io/gitea/modules/translation"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplTeamInviteMail base.TplName = "team_invite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MailTeamInvite sends team invites
|
|
|
|
func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_model.Team, invite *org_model.TeamInvite) error {
|
|
|
|
if setting.MailService == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:48:26 -05:00
|
|
|
org, err := user_model.GetUserByID(ctx, team.OrgID)
|
2022-10-19 08:40:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
locale := translation.NewLocale(inviter.Language)
|
|
|
|
|
|
|
|
subject := locale.Tr("mail.team_invite.subject", inviter.DisplayName(), org.DisplayName())
|
2023-07-04 23:41:32 -04:00
|
|
|
mailMeta := map[string]any{
|
2022-10-19 08:40:28 -04:00
|
|
|
"Inviter": inviter,
|
|
|
|
"Organization": org,
|
|
|
|
"Team": team,
|
|
|
|
"Invite": invite,
|
|
|
|
"Subject": subject,
|
|
|
|
// helper
|
|
|
|
"locale": locale,
|
|
|
|
"Str2html": templates.Str2html,
|
|
|
|
"DotEscape": templates.DotEscape,
|
|
|
|
}
|
|
|
|
|
|
|
|
var mailBody bytes.Buffer
|
|
|
|
if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplTeamInviteMail), mailMeta); err != nil {
|
|
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplTeamInviteMail)+"/body", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:23:52 -05:00
|
|
|
msg := NewMessage(invite.Email, subject, mailBody.String())
|
2022-10-19 08:40:28 -04:00
|
|
|
msg.Info = subject
|
|
|
|
|
|
|
|
SendAsync(msg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|