5
5
package models
6
6
7
7
import (
8
+ "bytes"
8
9
"fmt"
9
10
"html/template"
10
11
"path"
11
12
12
- "gopkg.in/gomail.v2"
13
- "gopkg.in/macaron.v1"
14
-
15
13
"code.gitea.io/gitea/modules/base"
16
14
"code.gitea.io/gitea/modules/log"
17
15
"code.gitea.io/gitea/modules/mailer"
18
16
"code.gitea.io/gitea/modules/markdown"
19
17
"code.gitea.io/gitea/modules/setting"
18
+ "gopkg.in/gomail.v2"
19
+ "gopkg.in/macaron.v1"
20
20
)
21
21
22
22
const (
@@ -31,27 +31,11 @@ const (
31
31
mailNotifyCollaborator base.TplName = "notify/collaborator"
32
32
)
33
33
34
- type mailRenderInterface interface {
35
- HTMLString (string , interface {}, ... macaron.HTMLOptions ) (string , error )
36
- }
37
-
38
- var mailRender mailRenderInterface
34
+ var templates * template.Template
39
35
40
36
// 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
55
39
}
56
40
57
41
// SendTestMail sends a test mail
@@ -67,13 +51,15 @@ func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject,
67
51
"ResetPwdCodeLives" : setting .Service .ResetPwdCodeLives / 60 ,
68
52
"Code" : code ,
69
53
}
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 )
73
59
return
74
60
}
75
61
76
- msg := mailer .NewMessage ([]string {u .Email }, subject , body )
62
+ msg := mailer .NewMessage ([]string {u .Email }, subject , content . String () )
77
63
msg .Info = fmt .Sprintf ("UID: %d, %s" , u .ID , info )
78
64
79
65
mailer .SendAsync (msg )
@@ -97,13 +83,15 @@ func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
97
83
"Code" : u .GenerateEmailActivateCode (email .Email ),
98
84
"Email" : email .Email ,
99
85
}
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 )
103
91
return
104
92
}
105
93
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 () )
107
95
msg .Info = fmt .Sprintf ("UID: %d, activate email" , u .ID )
108
96
109
97
mailer .SendAsync (msg )
@@ -114,13 +102,15 @@ func SendRegisterNotifyMail(c *macaron.Context, u *User) {
114
102
data := map [string ]interface {}{
115
103
"Username" : u .DisplayName (),
116
104
}
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 )
120
110
return
121
111
}
122
112
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 () )
124
114
msg .Info = fmt .Sprintf ("UID: %d, registration notify" , u .ID )
125
115
126
116
mailer .SendAsync (msg )
@@ -136,13 +126,15 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
136
126
"RepoName" : repoName ,
137
127
"Link" : repo .HTMLURL (),
138
128
}
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 )
142
134
return
143
135
}
144
136
145
- msg := mailer .NewMessage ([]string {u .Email }, subject , body )
137
+ msg := mailer .NewMessage ([]string {u .Email }, subject , content . String () )
146
138
msg .Info = fmt .Sprintf ("UID: %d, add collaborator" , u .ID )
147
139
148
140
mailer .SendAsync (msg )
@@ -161,11 +153,14 @@ func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []s
161
153
body := string (markdown .RenderSpecialLink ([]byte (issue .Content ), issue .Repo .HTMLURL (), issue .Repo .ComposeMetas ()))
162
154
data := composeTplData (subject , body , issue .HTMLURL ())
163
155
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 )
167
161
}
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 ())
169
164
msg .Info = fmt .Sprintf ("Subject: %s, %s" , subject , info )
170
165
return msg
171
166
}
0 commit comments