@@ -18,7 +18,8 @@ import (
18
18
"bytes"
19
19
"context"
20
20
"fmt"
21
- "html/template"
21
+ htmlTemplate "html/template"
22
+ textTemplate "text/template"
22
23
"mime/quotedprintable"
23
24
"net/smtp"
24
25
"strings"
@@ -41,15 +42,16 @@ func main() {
41
42
42
43
type smtpNotifier struct {
43
44
filter notifiers.EventFilter
44
- tmpl * template.Template
45
+ htmlTmpl * htmlTemplate.Template
46
+ textTmpl * textTemplate.Template
45
47
mcfg mailConfig
46
48
br notifiers.BindingResolver
47
49
tmplView * notifiers.TemplateView
48
50
}
49
51
50
52
type mailConfig struct {
51
- server , port , sender , from , password string
52
- recipients []string
53
+ server , port , sender , from , password , subject string
54
+ recipients []string
53
55
}
54
56
55
57
func (s * smtpNotifier ) SetUp (ctx context.Context , cfg * notifiers.Config , cfgTemplate string , sg notifiers.SecretGetter , br notifiers.BindingResolver ) error {
@@ -58,11 +60,19 @@ func (s *smtpNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, cfgTemp
58
60
return fmt .Errorf ("failed to create CELPredicate: %w" , err )
59
61
}
60
62
s .filter = prd
61
- tmpl , err := template .New ("email_template" ).Parse (cfgTemplate )
63
+ htmlTmpl , err := htmlTemplate .New ("email_template" ).Parse (cfgTemplate )
62
64
if err != nil {
63
65
return fmt .Errorf ("failed to parse HTML email template: %w" , err )
64
66
}
65
- s .tmpl = tmpl
67
+ s .htmlTmpl = htmlTmpl
68
+
69
+ if subject , subjectFound := cfg .Spec .Notification .Delivery ["subject" ]; subjectFound {
70
+ textTmpl , err := textTemplate .New ("subject_template" ).Parse (subject .(string ))
71
+ if err != nil {
72
+ return fmt .Errorf ("failed to parse TEXT subject template: %w" , err )
73
+ }
74
+ s .textTmpl = textTmpl
75
+ }
66
76
67
77
mcfg , err := getMailConfig (ctx , sg , cfg .Spec )
68
78
if err != nil {
@@ -175,11 +185,20 @@ func (s *smtpNotifier) buildEmail() (string, error) {
175
185
build .LogUrl = logURL
176
186
177
187
body := new (bytes.Buffer )
178
- if err := s .tmpl .Execute (body , s .tmplView ); err != nil {
188
+ if err := s .htmlTmpl .Execute (body , s .tmplView ); err != nil {
179
189
return "" , err
180
190
}
181
191
182
192
subject := fmt .Sprintf ("Cloud Build [%s]: %s" , build .ProjectId , build .Id )
193
+ if s .textTmpl != nil {
194
+ subjectTmpl := new (bytes.Buffer )
195
+ if err := s .textTmpl .Execute (subjectTmpl , s .tmplView ); err != nil {
196
+ return "" , err
197
+ }
198
+
199
+ // Escape any string formatter
200
+ subject = strings .Join (strings .Fields (subjectTmpl .String ()), " " )
201
+ }
183
202
184
203
header := make (map [string ]string )
185
204
if s .mcfg .from != s .mcfg .sender {
0 commit comments