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