2016-07-15 09:36:39 -07:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-09-23 22:02:49 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-07-15 09:36:39 -07:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
package mailer
|
2016-07-15 09:36:39 -07:00
|
|
|
|
|
|
|
import (
|
2016-12-06 10:58:31 -07:00
|
|
|
"bytes"
|
2016-07-15 09:36:39 -07:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2019-11-07 06:34:28 -07:00
|
|
|
"mime"
|
2016-07-15 09:36:39 -07:00
|
|
|
"path"
|
2019-11-07 06:34:28 -07:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
texttmpl "text/template"
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
"code.gitea.io/gitea/models"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-09-16 10:17:57 -07:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-09-20 22:20:14 -07:00
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 07:46:21 -07:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-08-23 09:40:30 -07:00
|
|
|
|
2016-12-06 10:58:31 -07:00
|
|
|
"gopkg.in/gomail.v2"
|
2016-07-15 09:36:39 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-25 01:11:12 -07: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 09:36:39 -07:00
|
|
|
|
2016-11-25 01:11:12 -07:00
|
|
|
mailNotifyCollaborator base.TplName = "notify/collaborator"
|
2019-11-07 06:34:28 -07:00
|
|
|
|
|
|
|
// There's no actual limit for subject in RFC 5322
|
|
|
|
mailMaxSubjectRunes = 256
|
2016-07-15 09:36:39 -07:00
|
|
|
)
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
var (
|
|
|
|
bodyTemplates *template.Template
|
|
|
|
subjectTemplates *texttmpl.Template
|
|
|
|
subjectRemoveSpaces = regexp.MustCompile(`[\s]+`)
|
|
|
|
)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-05-14 06:52:18 -07:00
|
|
|
// InitMailRender initializes the mail renderer
|
2019-11-07 06:34:28 -07:00
|
|
|
func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) {
|
|
|
|
subjectTemplates = subjectTpl
|
|
|
|
bodyTemplates = bodyTpl
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:11:12 -07:00
|
|
|
// SendTestMail sends a test mail
|
2016-07-15 09:36:39 -07:00
|
|
|
func SendTestMail(email string) error {
|
2019-09-23 22:02:49 -07:00
|
|
|
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:11:12 -07:00
|
|
|
// SendUserMail sends a mail to the user
|
2019-09-23 22:02:49 -07:00
|
|
|
func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) {
|
2016-07-15 09:36:39 -07:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 00:52:48 -07:00
|
|
|
"DisplayName": u.DisplayName(),
|
2019-08-15 07:46:21 -07:00
|
|
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
|
|
|
|
"ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
|
2016-07-15 09:36:39 -07:00
|
|
|
"Code": code,
|
|
|
|
}
|
2016-12-06 10:58:31 -07:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
if err := bodyTemplates.ExecuteTemplate(&content, string(tpl), data); err != nil {
|
2019-04-02 00:48:31 -07:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-15 09:36:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
2016-07-23 10:08:22 -07:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
SendAsync(msg)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 15:53:54 -07:00
|
|
|
// Locale represents an interface to translation
|
|
|
|
type Locale interface {
|
|
|
|
Language() string
|
|
|
|
Tr(string, ...interface{}) string
|
|
|
|
}
|
|
|
|
|
2017-05-13 19:38:30 -07:00
|
|
|
// SendActivateAccountMail sends an activation mail to the user (new user registration)
|
2019-09-23 22:02:49 -07:00
|
|
|
func SendActivateAccountMail(locale Locale, u *models.User) {
|
2019-05-13 15:53:54 -07:00
|
|
|
SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2016-11-25 01:11:12 -07:00
|
|
|
// SendResetPasswordMail sends a password reset mail to the user
|
2019-09-23 22:02:49 -07:00
|
|
|
func SendResetPasswordMail(locale Locale, u *models.User) {
|
2019-05-13 15:53:54 -07:00
|
|
|
SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2017-05-13 19:38:30 -07:00
|
|
|
// SendActivateEmailMail sends confirmation email to confirm new email address
|
2019-09-23 22:02:49 -07:00
|
|
|
func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) {
|
2016-07-15 09:36:39 -07:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 00:52:48 -07:00
|
|
|
"DisplayName": u.DisplayName(),
|
2019-08-15 07:46:21 -07:00
|
|
|
"ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
|
2016-07-15 09:36:39 -07:00
|
|
|
"Code": u.GenerateEmailActivateCode(email.Email),
|
|
|
|
"Email": email.Email,
|
|
|
|
}
|
2016-12-06 10:58:31 -07:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
if err := bodyTemplates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
|
2019-04-02 00:48:31 -07:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-15 09:36:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
|
2016-07-23 10:08:22 -07:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
SendAsync(msg)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
2019-09-23 22:02:49 -07: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 09:36:39 -07:00
|
|
|
data := map[string]interface{}{
|
2019-04-04 00:52:48 -07:00
|
|
|
"DisplayName": u.DisplayName(),
|
|
|
|
"Username": u.Name,
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
2016-12-06 10:58:31 -07:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
if err := bodyTemplates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
|
2019-04-02 00:48:31 -07:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-15 09:36:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
|
2016-07-23 10:08:22 -07:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
SendAsync(msg)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendCollaboratorMail sends mail notification to new collaborator.
|
2019-09-23 22:02:49 -07:00
|
|
|
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
|
2016-07-15 09:36:39 -07: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 10:19:09 -07:00
|
|
|
"Link": repo.HTMLURL(),
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
2016-12-06 10:58:31 -07:00
|
|
|
|
|
|
|
var content bytes.Buffer
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
if err := bodyTemplates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
|
2019-04-02 00:48:31 -07:00
|
|
|
log.Error("Template: %v", err)
|
2016-07-15 09:36:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
msg := NewMessage([]string{u.Email}, subject, content.String())
|
2016-07-23 10:08:22 -07:00
|
|
|
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
SendAsync(msg)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionType models.ActionType, fromMention bool,
|
|
|
|
content string, comment *models.Comment, tos []string, info string) *Message {
|
|
|
|
|
|
|
|
if err := issue.LoadPullRequest(); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
subject string
|
|
|
|
link string
|
|
|
|
prefix string
|
|
|
|
// Fall back subject for bad templates, make sure subject is never empty
|
2019-11-15 05:59:21 -07:00
|
|
|
fallback string
|
|
|
|
reviewComments []*models.Comment
|
2019-11-07 06:34:28 -07:00
|
|
|
)
|
2016-07-15 09:36:39 -07:00
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
commentType := models.CommentTypeComment
|
2019-07-17 12:02:42 -07:00
|
|
|
if comment != nil {
|
2019-11-07 06:34:28 -07:00
|
|
|
prefix = "Re: "
|
|
|
|
commentType = comment.Type
|
|
|
|
link = issue.HTMLURL() + "#" + comment.HashTag()
|
2019-07-17 12:02:42 -07:00
|
|
|
} else {
|
2019-11-07 06:34:28 -07:00
|
|
|
link = issue.HTMLURL()
|
2019-06-12 12:41:28 -07:00
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
|
2019-11-15 05:59:21 -07:00
|
|
|
reviewType := models.ReviewTypeComment
|
|
|
|
if comment != nil && comment.Review != nil {
|
|
|
|
reviewType = comment.Review.Type
|
|
|
|
}
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
fallback = prefix + fallbackMailSubject(issue)
|
|
|
|
|
|
|
|
// This is the body of the new issue or comment, not the mail body
|
2017-11-03 02:23:17 -07:00
|
|
|
body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
|
2017-05-24 19:38:56 -07:00
|
|
|
|
2019-11-15 05:59:21 -07:00
|
|
|
actType, actName, tplName := actionToTemplate(issue, actionType, commentType, reviewType)
|
|
|
|
|
|
|
|
if comment != nil && comment.Review != nil {
|
|
|
|
reviewComments = make([]*models.Comment, 0, 10)
|
|
|
|
for _, lines := range comment.Review.CodeComments {
|
|
|
|
for _, comments := range lines {
|
|
|
|
reviewComments = append(reviewComments, comments...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
|
|
|
|
mailMeta := map[string]interface{}{
|
|
|
|
"FallbackSubject": fallback,
|
|
|
|
"Body": body,
|
|
|
|
"Link": link,
|
|
|
|
"Issue": issue,
|
|
|
|
"Comment": comment,
|
|
|
|
"IsPull": issue.IsPull,
|
|
|
|
"User": issue.Repo.MustOwner(),
|
|
|
|
"Repo": issue.Repo.FullName(),
|
|
|
|
"Doer": doer,
|
|
|
|
"IsMention": fromMention,
|
|
|
|
"SubjectPrefix": prefix,
|
|
|
|
"ActionType": actType,
|
|
|
|
"ActionName": actName,
|
2019-11-15 05:59:21 -07:00
|
|
|
"ReviewComments": reviewComments,
|
2019-11-07 06:34:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var mailSubject bytes.Buffer
|
|
|
|
if err := subjectTemplates.ExecuteTemplate(&mailSubject, string(tplName), mailMeta); err == nil {
|
|
|
|
subject = sanitizeSubject(mailSubject.String())
|
2017-05-24 19:38:56 -07:00
|
|
|
} else {
|
2019-11-07 06:34:28 -07:00
|
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplName)+"/subject", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subject == "" {
|
|
|
|
subject = fallback
|
2017-05-24 19:38:56 -07:00
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
mailMeta["Subject"] = subject
|
2016-12-06 10:58:31 -07:00
|
|
|
|
2017-11-03 02:23:17 -07:00
|
|
|
var mailBody bytes.Buffer
|
2016-12-06 10:58:31 -07:00
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplName), mailMeta); err != nil {
|
|
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplName)+"/body", err)
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
2016-12-06 10:58:31 -07:00
|
|
|
|
2019-09-23 22:02:49 -07:00
|
|
|
msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
|
2016-07-15 09:36:39 -07:00
|
|
|
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
2019-07-17 12:02:42 -07: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 09:36:39 -07:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
func sanitizeSubject(subject string) string {
|
|
|
|
runes := []rune(strings.TrimSpace(subjectRemoveSpaces.ReplaceAllLiteralString(subject, " ")))
|
|
|
|
if len(runes) > mailMaxSubjectRunes {
|
|
|
|
runes = runes[:mailMaxSubjectRunes]
|
|
|
|
}
|
|
|
|
// Encode non-ASCII characters
|
|
|
|
return mime.QEncoding.Encode("utf-8", string(runes))
|
|
|
|
}
|
|
|
|
|
2016-07-15 09:36:39 -07:00
|
|
|
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
2019-11-07 06:34:28 -07:00
|
|
|
func SendIssueCommentMail(issue *models.Issue, doer *models.User, actionType models.ActionType, content string, comment *models.Comment, tos []string) {
|
2016-07-15 09:36:39 -07:00
|
|
|
if len(tos) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-07 06:34:28 -07:00
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, actionType, false, content, comment, tos, "issue comment"))
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
2019-11-07 06:34:28 -07:00
|
|
|
func SendIssueMentionMail(issue *models.Issue, doer *models.User, actionType models.ActionType, content string, comment *models.Comment, tos []string) {
|
2016-07-15 09:36:39 -07:00
|
|
|
if len(tos) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, actionType, true, content, comment, tos, "issue mention"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// actionToTemplate returns the type and name of the action facing the user
|
|
|
|
// (slightly different from models.ActionType) and the name of the template to use (based on availability)
|
2019-11-15 05:59:21 -07:00
|
|
|
func actionToTemplate(issue *models.Issue, actionType models.ActionType,
|
|
|
|
commentType models.CommentType, reviewType models.ReviewType) (typeName, name, template string) {
|
2019-11-07 06:34:28 -07:00
|
|
|
if issue.IsPull {
|
|
|
|
typeName = "pull"
|
|
|
|
} else {
|
|
|
|
typeName = "issue"
|
|
|
|
}
|
|
|
|
switch actionType {
|
|
|
|
case models.ActionCreateIssue, models.ActionCreatePullRequest:
|
|
|
|
name = "new"
|
|
|
|
case models.ActionCommentIssue:
|
|
|
|
name = "comment"
|
|
|
|
case models.ActionCloseIssue, models.ActionClosePullRequest:
|
|
|
|
name = "close"
|
|
|
|
case models.ActionReopenIssue, models.ActionReopenPullRequest:
|
|
|
|
name = "reopen"
|
|
|
|
case models.ActionMergePullRequest:
|
|
|
|
name = "merge"
|
|
|
|
default:
|
|
|
|
switch commentType {
|
|
|
|
case models.CommentTypeReview:
|
2019-11-15 05:59:21 -07:00
|
|
|
switch reviewType {
|
|
|
|
case models.ReviewTypeApprove:
|
|
|
|
name = "approve"
|
|
|
|
case models.ReviewTypeReject:
|
|
|
|
name = "reject"
|
|
|
|
default:
|
|
|
|
name = "review"
|
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
case models.CommentTypeCode:
|
|
|
|
name = "code"
|
|
|
|
case models.CommentTypeAssignees:
|
|
|
|
name = "assigned"
|
|
|
|
default:
|
|
|
|
name = "default"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template = typeName + "/" + name
|
|
|
|
ok := bodyTemplates.Lookup(template) != nil
|
|
|
|
if !ok && typeName != "issue" {
|
|
|
|
template = "issue/" + name
|
|
|
|
ok = bodyTemplates.Lookup(template) != nil
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
template = typeName + "/default"
|
|
|
|
ok = bodyTemplates.Lookup(template) != nil
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
template = "issue/default"
|
|
|
|
}
|
|
|
|
return
|
2016-07-15 09:36:39 -07:00
|
|
|
}
|
2019-10-25 07:46:37 -07:00
|
|
|
|
|
|
|
// SendIssueAssignedMail composes and sends issue assigned email
|
|
|
|
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) {
|
2019-11-07 06:34:28 -07:00
|
|
|
SendAsync(composeIssueCommentMessage(issue, doer, models.ActionType(0), false, content, comment, tos, "issue assigned"))
|
2019-10-25 07:46:37 -07:00
|
|
|
}
|