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