mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
6.0 KiB
206 lines
6.0 KiB
9 years ago
|
// 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.
|
||
|
|
||
8 years ago
|
package mailer
|
||
9 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
9 years ago
|
"gopkg.in/gomail.v2"
|
||
|
"gopkg.in/macaron.v1"
|
||
|
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/base"
|
||
|
"github.com/gogits/gogs/pkg/markup"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
|
MAIL_AUTH_ACTIVATE base.TplName = "auth/activate"
|
||
|
MAIL_AUTH_ACTIVATE_EMAIL base.TplName = "auth/activate_email"
|
||
|
MAIL_AUTH_RESET_PASSWORD base.TplName = "auth/reset_passwd"
|
||
|
MAIL_AUTH_REGISTER_NOTIFY base.TplName = "auth/register_notify"
|
||
|
|
||
|
MAIL_ISSUE_COMMENT base.TplName = "issue/comment"
|
||
|
MAIL_ISSUE_MENTION base.TplName = "issue/mention"
|
||
|
|
||
|
MAIL_NOTIFY_COLLABORATOR base.TplName = "notify/collaborator"
|
||
|
)
|
||
|
|
||
|
type MailRender interface {
|
||
|
HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
|
||
|
}
|
||
|
|
||
|
var mailRender MailRender
|
||
|
|
||
|
func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
|
||
|
opt := &macaron.RenderOptions{
|
||
|
Directory: dir,
|
||
|
AppendDirectories: []string{appendDir},
|
||
|
Funcs: funcMap,
|
||
|
Extensions: []string{".tmpl", ".html"},
|
||
|
}
|
||
|
ts := macaron.NewTemplateSet()
|
||
|
ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
|
||
|
|
||
|
mailRender = &macaron.TplRender{
|
||
|
TemplateSet: ts,
|
||
|
Opt: opt,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func SendTestMail(email string) error {
|
||
8 years ago
|
return gomail.Send(&Sender{}, NewMessage([]string{email}, "Gogs Test Email!", "Gogs Test Email!").Message)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
/*
|
||
|
Setup interfaces of used methods in mail to avoid cycle import.
|
||
|
*/
|
||
|
|
||
|
type User interface {
|
||
|
ID() int64
|
||
|
DisplayName() string
|
||
|
Email() string
|
||
|
GenerateActivateCode() string
|
||
|
GenerateEmailActivateCode(string) string
|
||
|
}
|
||
|
|
||
|
type Repository interface {
|
||
|
FullName() string
|
||
|
HTMLURL() string
|
||
|
ComposeMetas() map[string]string
|
||
|
}
|
||
|
|
||
|
type Issue interface {
|
||
|
MailSubject() string
|
||
|
Content() string
|
||
|
HTMLURL() string
|
||
|
}
|
||
|
|
||
|
func SendUserMail(c *macaron.Context, u User, tpl base.TplName, code, subject, info string) {
|
||
9 years ago
|
data := map[string]interface{}{
|
||
|
"Username": u.DisplayName(),
|
||
|
"ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
|
||
|
"ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
|
||
|
"Code": code,
|
||
|
}
|
||
|
body, err := mailRender.HTMLString(string(tpl), data)
|
||
|
if err != nil {
|
||
|
log.Error(3, "HTMLString: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
msg := NewMessage([]string{u.Email()}, subject, body)
|
||
|
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info)
|
||
9 years ago
|
|
||
8 years ago
|
SendAsync(msg)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func SendActivateAccountMail(c *macaron.Context, u User) {
|
||
9 years ago
|
SendUserMail(c, u, MAIL_AUTH_ACTIVATE, u.GenerateActivateCode(), c.Tr("mail.activate_account"), "activate account")
|
||
|
}
|
||
|
|
||
8 years ago
|
func SendResetPasswordMail(c *macaron.Context, u User) {
|
||
9 years ago
|
SendUserMail(c, u, MAIL_AUTH_RESET_PASSWORD, u.GenerateActivateCode(), c.Tr("mail.reset_password"), "reset password")
|
||
|
}
|
||
|
|
||
|
// SendActivateAccountMail sends confirmation email.
|
||
8 years ago
|
func SendActivateEmailMail(c *macaron.Context, u User, email string) {
|
||
9 years ago
|
data := map[string]interface{}{
|
||
|
"Username": u.DisplayName(),
|
||
|
"ActiveCodeLives": setting.Service.ActiveCodeLives / 60,
|
||
8 years ago
|
"Code": u.GenerateEmailActivateCode(email),
|
||
|
"Email": email,
|
||
9 years ago
|
}
|
||
|
body, err := mailRender.HTMLString(string(MAIL_AUTH_ACTIVATE_EMAIL), data)
|
||
|
if err != nil {
|
||
|
log.Error(3, "HTMLString: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body)
|
||
|
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID())
|
||
9 years ago
|
|
||
8 years ago
|
SendAsync(msg)
|
||
9 years ago
|
}
|
||
|
|
||
|
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
||
8 years ago
|
func SendRegisterNotifyMail(c *macaron.Context, u User) {
|
||
9 years ago
|
data := map[string]interface{}{
|
||
|
"Username": u.DisplayName(),
|
||
|
}
|
||
|
body, err := mailRender.HTMLString(string(MAIL_AUTH_REGISTER_NOTIFY), data)
|
||
|
if err != nil {
|
||
|
log.Error(3, "HTMLString: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body)
|
||
|
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID())
|
||
9 years ago
|
|
||
8 years ago
|
SendAsync(msg)
|
||
9 years ago
|
}
|
||
|
|
||
|
// SendCollaboratorMail sends mail notification to new collaborator.
|
||
8 years ago
|
func SendCollaboratorMail(u, doer User, repo Repository) {
|
||
|
subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repo.FullName())
|
||
9 years ago
|
|
||
|
data := map[string]interface{}{
|
||
|
"Subject": subject,
|
||
8 years ago
|
"RepoName": repo.FullName(),
|
||
8 years ago
|
"Link": repo.HTMLURL(),
|
||
9 years ago
|
}
|
||
|
body, err := mailRender.HTMLString(string(MAIL_NOTIFY_COLLABORATOR), data)
|
||
|
if err != nil {
|
||
|
log.Error(3, "HTMLString: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
msg := NewMessage([]string{u.Email()}, subject, body)
|
||
|
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID())
|
||
9 years ago
|
|
||
8 years ago
|
SendAsync(msg)
|
||
9 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
8 years ago
|
func composeIssueMessage(issue Issue, repo Repository, doer User, tplName base.TplName, tos []string, info string) *Message {
|
||
9 years ago
|
subject := issue.MailSubject()
|
||
8 years ago
|
body := string(markup.RenderSpecialLink([]byte(issue.Content()), repo.HTMLURL(), repo.ComposeMetas()))
|
||
8 years ago
|
data := composeTplData(subject, body, issue.HTMLURL())
|
||
9 years ago
|
data["Doer"] = doer
|
||
|
content, err := mailRender.HTMLString(string(tplName), data)
|
||
|
if err != nil {
|
||
|
log.Error(3, "HTMLString (%s): %v", tplName, err)
|
||
|
}
|
||
8 years ago
|
from := gomail.NewMessage().FormatAddress(setting.MailService.FromEmail, doer.DisplayName())
|
||
8 years ago
|
msg := NewMessageFrom(tos, from, subject, content)
|
||
9 years ago
|
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
||
|
return msg
|
||
|
}
|
||
|
|
||
|
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
||
8 years ago
|
func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) {
|
||
9 years ago
|
if len(tos) == 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment"))
|
||
9 years ago
|
}
|
||
|
|
||
|
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
||
8 years ago
|
func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) {
|
||
9 years ago
|
if len(tos) == 0 {
|
||
|
return
|
||
|
}
|
||
8 years ago
|
SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention"))
|
||
9 years ago
|
}
|