55package models
66
77import (
8+ "bytes"
89 "fmt"
910 "html/template"
1011 "path"
1112
12- "gopkg.in/gomail.v2"
13- "gopkg.in/macaron.v1"
14-
1513 "code.gitea.io/gitea/modules/base"
1614 "code.gitea.io/gitea/modules/log"
1715 "code.gitea.io/gitea/modules/mailer"
1816 "code.gitea.io/gitea/modules/markdown"
1917 "code.gitea.io/gitea/modules/setting"
18+ "gopkg.in/gomail.v2"
19+ "gopkg.in/macaron.v1"
2020)
2121
2222const (
@@ -31,27 +31,11 @@ const (
3131 mailNotifyCollaborator base.TplName = "notify/collaborator"
3232)
3333
34- type mailRenderInterface interface {
35- HTMLString (string , interface {}, ... macaron.HTMLOptions ) (string , error )
36- }
37-
38- var mailRender mailRenderInterface
34+ var templates * template.Template
3935
4036// InitMailRender initializes the macaron mail renderer
41- func InitMailRender (dir , appendDir string , funcMap []template.FuncMap ) {
42- opt := & macaron.RenderOptions {
43- Directory : dir ,
44- AppendDirectories : []string {appendDir },
45- Funcs : funcMap ,
46- Extensions : []string {".tmpl" , ".html" },
47- }
48- ts := macaron .NewTemplateSet ()
49- ts .Set (macaron .DEFAULT_TPL_SET_NAME , opt )
50-
51- mailRender = & macaron.TplRender {
52- TemplateSet : ts ,
53- Opt : opt ,
54- }
37+ func InitMailRender (tmpls * template.Template ) {
38+ templates = tmpls
5539}
5640
5741// SendTestMail sends a test mail
@@ -67,13 +51,15 @@ func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject,
6751 "ResetPwdCodeLives" : setting .Service .ResetPwdCodeLives / 60 ,
6852 "Code" : code ,
6953 }
70- body , err := mailRender .HTMLString (string (tpl ), data )
71- if err != nil {
72- log .Error (3 , "HTMLString: %v" , err )
54+
55+ var content bytes.Buffer
56+
57+ if err := templates .ExecuteTemplate (& content , string (tpl ), data ); err != nil {
58+ log .Error (3 , "Template: %v" , err )
7359 return
7460 }
7561
76- msg := mailer .NewMessage ([]string {u .Email }, subject , body )
62+ msg := mailer .NewMessage ([]string {u .Email }, subject , content . String () )
7763 msg .Info = fmt .Sprintf ("UID: %d, %s" , u .ID , info )
7864
7965 mailer .SendAsync (msg )
@@ -97,13 +83,15 @@ func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
9783 "Code" : u .GenerateEmailActivateCode (email .Email ),
9884 "Email" : email .Email ,
9985 }
100- body , err := mailRender .HTMLString (string (mailAuthActivateEmail ), data )
101- if err != nil {
102- log .Error (3 , "HTMLString: %v" , err )
86+
87+ var content bytes.Buffer
88+
89+ if err := templates .ExecuteTemplate (& content , string (mailAuthActivateEmail ), data ); err != nil {
90+ log .Error (3 , "Template: %v" , err )
10391 return
10492 }
10593
106- msg := mailer .NewMessage ([]string {email .Email }, c .Tr ("mail.activate_email" ), body )
94+ msg := mailer .NewMessage ([]string {email .Email }, c .Tr ("mail.activate_email" ), content . String () )
10795 msg .Info = fmt .Sprintf ("UID: %d, activate email" , u .ID )
10896
10997 mailer .SendAsync (msg )
@@ -114,13 +102,15 @@ func SendRegisterNotifyMail(c *macaron.Context, u *User) {
114102 data := map [string ]interface {}{
115103 "Username" : u .DisplayName (),
116104 }
117- body , err := mailRender .HTMLString (string (mailAuthRegisterNotify ), data )
118- if err != nil {
119- log .Error (3 , "HTMLString: %v" , err )
105+
106+ var content bytes.Buffer
107+
108+ if err := templates .ExecuteTemplate (& content , string (mailAuthRegisterNotify ), data ); err != nil {
109+ log .Error (3 , "Template: %v" , err )
120110 return
121111 }
122112
123- msg := mailer .NewMessage ([]string {u .Email }, c .Tr ("mail.register_notify" ), body )
113+ msg := mailer .NewMessage ([]string {u .Email }, c .Tr ("mail.register_notify" ), content . String () )
124114 msg .Info = fmt .Sprintf ("UID: %d, registration notify" , u .ID )
125115
126116 mailer .SendAsync (msg )
@@ -136,13 +126,15 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
136126 "RepoName" : repoName ,
137127 "Link" : repo .HTMLURL (),
138128 }
139- body , err := mailRender .HTMLString (string (mailNotifyCollaborator ), data )
140- if err != nil {
141- log .Error (3 , "HTMLString: %v" , err )
129+
130+ var content bytes.Buffer
131+
132+ if err := templates .ExecuteTemplate (& content , string (mailNotifyCollaborator ), data ); err != nil {
133+ log .Error (3 , "Template: %v" , err )
142134 return
143135 }
144136
145- msg := mailer .NewMessage ([]string {u .Email }, subject , body )
137+ msg := mailer .NewMessage ([]string {u .Email }, subject , content . String () )
146138 msg .Info = fmt .Sprintf ("UID: %d, add collaborator" , u .ID )
147139
148140 mailer .SendAsync (msg )
@@ -161,11 +153,14 @@ func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []s
161153 body := string (markdown .RenderSpecialLink ([]byte (issue .Content ), issue .Repo .HTMLURL (), issue .Repo .ComposeMetas ()))
162154 data := composeTplData (subject , body , issue .HTMLURL ())
163155 data ["Doer" ] = doer
164- content , err := mailRender .HTMLString (string (tplName ), data )
165- if err != nil {
166- log .Error (3 , "HTMLString (%s): %v" , tplName , err )
156+
157+ var content bytes.Buffer
158+
159+ if err := templates .ExecuteTemplate (& content , string (tplName ), data ); err != nil {
160+ log .Error (3 , "Template: %v" , err )
167161 }
168- msg := mailer .NewMessageFrom (tos , fmt .Sprintf (`"%s" <%s>` , doer .DisplayName (), setting .MailService .FromEmail ), subject , content )
162+
163+ msg := mailer .NewMessageFrom (tos , fmt .Sprintf (`"%s" <%s>` , doer .DisplayName (), setting .MailService .FromEmail ), subject , content .String ())
169164 msg .Info = fmt .Sprintf ("Subject: %s, %s" , subject , info )
170165 return msg
171166}
0 commit comments