2016-07-16 00:36:39 +08:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-09-24 13:02:49 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-07-16 00:36:39 +08:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
package mailer
|
2016-07-16 00:36:39 +08:00
|
|
|
|
|
|
|
import (
|
2016-12-07 01:58:31 +08:00
|
|
|
"bytes"
|
2016-07-16 00:36:39 +08:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"path"
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-09-17 01:17:57 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-09-21 13:20:14 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-08-24 00:40:30 +08:00
|
|
|
|
2016-12-07 01:58:31 +08:00
|
|
|
"gopkg.in/gomail.v2"
|
2016-07-16 00:36:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-25 16:11:12 +08:00
|
|
|
mailAuthActivate base.TplName = "auth/activate"
|
|
|
|
mailAuthActivateEmail base.TplName = "auth/activate_email"
|
|
|
|
mailAuthResetPassword base.TplName = "auth/reset_passwd"
|
|
|
|
mailAuthRegisterNotify base.TplName = "auth/register_notify"
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-10-25 22:46:37 +08:00
|
|
|
mailIssueComment base.TplName = "issue/comment"
|
|
|
|
mailIssueMention base.TplName = "issue/mention"
|
|
|
|
mailIssueAssigned base.TplName = "issue/assigned"
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2016-11-25 16:11:12 +08:00
|
|
|
mailNotifyCollaborator base.TplName = "notify/collaborator"
|
2016-07-16 00:36:39 +08:00
|
|
|
)
|
|
|
|
|
2016-12-07 01:58:31 +08:00
|
|
|
var templates *template.Template
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-05-14 21:52:18 +08:00
|
|
|
// InitMailRender initializes the mail renderer
|
2016-12-07 01:58:31 +08:00
|
|
|
func InitMailRender(tmpls *template.Template) {
|
|
|
|
templates = tmpls
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:11:12 +08:00
|
|
|
// SendTestMail sends a test mail
|
2016-07-16 00:36:39 +08:00
|
|
|
func SendTestMail(email string) error {
|
2019-09-24 13:02:49 +08:00
|
|
|
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:11:12 +08:00
|
|
|
// SendUserMail sends a mail to the user
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) {
|
2016-07-16 00:36:39 +08:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 15:52:48 +08:00
|
|
|
"DisplayName": u.DisplayName(),
|
2019-08-15 22:46:21 +08:00
|
|
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
|
|
|
|
"ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
|
2016-07-16 00:36:39 +08:00
|
|
|
"Code": code,
|
|
|
|
}
|
2016-12-07 01:58:31 +08:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
|
|
|
if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-16 00:36:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
2016-07-24 01:08:22 +08:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(msg)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-14 06:53:54 +08:00
|
|
|
// Locale represents an interface to translation
|
|
|
|
type Locale interface {
|
|
|
|
Language() string
|
|
|
|
Tr(string, ...interface{}) string
|
|
|
|
}
|
|
|
|
|
2017-05-14 10:38:30 +08:00
|
|
|
// SendActivateAccountMail sends an activation mail to the user (new user registration)
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendActivateAccountMail(locale Locale, u *models.User) {
|
2019-05-14 06:53:54 +08:00
|
|
|
SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 16:11:12 +08:00
|
|
|
// SendResetPasswordMail sends a password reset mail to the user
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendResetPasswordMail(locale Locale, u *models.User) {
|
2019-05-14 06:53:54 +08:00
|
|
|
SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
2017-05-14 10:38:30 +08:00
|
|
|
// SendActivateEmailMail sends confirmation email to confirm new email address
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) {
|
2016-07-16 00:36:39 +08:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 15:52:48 +08:00
|
|
|
"DisplayName": u.DisplayName(),
|
2019-08-15 22:46:21 +08:00
|
|
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
|
2016-07-16 00:36:39 +08:00
|
|
|
"Code": u.GenerateEmailActivateCode(email.Email),
|
|
|
|
"Email": email.Email,
|
|
|
|
}
|
2016-12-07 01:58:31 +08:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
|
|
|
if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-16 00:36:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
|
2016-07-24 01:08:22 +08:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(msg)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendRegisterNotifyMail(locale Locale, u *models.User) {
|
|
|
|
if setting.MailService == nil {
|
|
|
|
log.Warn("SendRegisterNotifyMail is being invoked but mail service hasn't been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-16 00:36:39 +08:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 15:52:48 +08:00
|
|
|
"DisplayName": u.DisplayName(),
|
|
|
|
"Username": u.Name,
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
2016-12-07 01:58:31 +08:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
|
|
|
if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-16 00:36:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
|
2016-07-24 01:08:22 +08:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(msg)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendCollaboratorMail sends mail notification to new collaborator.
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
|
2016-07-16 00:36:39 +08:00
|
|
|
repoName := path.Join(repo.Owner.Name, repo.Name)
|
|
|
|
subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"Subject": subject,
|
|
|
|
"RepoName": repoName,
|
2016-08-17 01:19:09 +08:00
|
|
|
"Link": repo.HTMLURL(),
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
2016-12-07 01:58:31 +08:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
|
|
|
if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-16 00:36:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
2016-07-24 01:08:22 +08:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(msg)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func composeTplData(subject, body, link string) map[string]interface{} {
|
|
|
|
data := make(map[string]interface{}, 10)
|
|
|
|
data["Subject"] = subject
|
|
|
|
data["Body"] = body
|
|
|
|
data["Link"] = link
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tplName base.TplName, tos []string, info string) *Message {
|
2019-07-18 03:02:42 +08:00
|
|
|
var subject string
|
|
|
|
if comment != nil {
|
2019-09-24 13:02:49 +08:00
|
|
|
subject = "Re: " + mailSubject(issue)
|
2019-07-18 03:02:42 +08:00
|
|
|
} else {
|
2019-09-24 13:02:49 +08:00
|
|
|
subject = mailSubject(issue)
|
2019-07-18 03:02:42 +08:00
|
|
|
}
|
2019-06-13 03:41:28 +08:00
|
|
|
err := issue.LoadRepo()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
}
|
2017-11-03 17:23:17 +08:00
|
|
|
body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
|
2017-05-25 10:38:56 +08:00
|
|
|
|
2019-06-13 03:41:28 +08:00
|
|
|
var data = make(map[string]interface{}, 10)
|
2017-05-25 10:38:56 +08:00
|
|
|
if comment != nil {
|
|
|
|
data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
|
|
|
|
} else {
|
|
|
|
data = composeTplData(subject, body, issue.HTMLURL())
|
|
|
|
}
|
2016-07-16 00:36:39 +08:00
|
|
|
data["Doer"] = doer
|
2019-10-25 22:46:37 +08:00
|
|
|
data["Issue"] = issue
|
2016-12-07 01:58:31 +08:00
|
|
|
|
2017-11-03 17:23:17 +08:00
|
|
|
var mailBody bytes.Buffer
|
2016-12-07 01:58:31 +08:00
|
|
|
|
2017-11-03 17:23:17 +08:00
|
|
|
if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
2016-12-07 01:58:31 +08:00
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
|
2016-07-16 00:36:39 +08:00
|
|
|
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
2019-07-18 03:02:42 +08:00
|
|
|
|
|
|
|
// Set Message-ID on first message so replies know what to reference
|
|
|
|
if comment == nil {
|
|
|
|
msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">")
|
|
|
|
} else {
|
|
|
|
msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">")
|
|
|
|
msg.SetHeader("References", "<"+issue.ReplyReference()+">")
|
|
|
|
}
|
|
|
|
|
2016-07-16 00:36:39 +08:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendIssueCommentMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
2016-07-16 00:36:39 +08:00
|
|
|
if len(tos) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
2019-09-24 13:02:49 +08:00
|
|
|
func SendIssueMentionMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
2016-07-16 00:36:39 +08:00
|
|
|
if len(tos) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-24 13:02:49 +08:00
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
|
2016-07-16 00:36:39 +08:00
|
|
|
}
|
2019-10-25 22:46:37 +08:00
|
|
|
|
|
|
|
// SendIssueAssignedMail composes and sends issue assigned email
|
|
|
|
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueAssigned, tos, "issue assigned"))
|
|
|
|
}
|