-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathmsg_template.go
More file actions
executable file
·609 lines (586 loc) · 24.6 KB
/
Copy pathmsg_template.go
File metadata and controls
executable file
·609 lines (586 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// SPDX-FileCopyrightText: The go-mail Authors
//
// SPDX-License-Identifier: MIT
//go:build !gomailnotpl
package mail
import (
"bytes"
"errors"
"fmt"
ht "html/template"
tt "text/template"
)
// SetBodyTextTemplate sets the body of the message from a given text/template.Template pointer.
//
// This method sets the body of the message using the provided text template and data. The content type
// will be set to "text/plain" automatically. The method executes the template with the provided data
// and writes the output to the message body. If the template is nil or fails to execute, an error will
// be returned.
//
// Parameters:
// - tpl: A pointer to the text/template.Template to be used for the message body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) SetBodyTextTemplate(tpl *tt.Template, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextPlain, writeFunc, opts...)
return nil
}
// SetBodyNamedTextTemplate sets the body of the message from a template associated with a given
// text/template.Template pointer that has the given name.
//
// This method sets the body of the message using the provided text template, name and data. The content type
// will be set to "text/plain" automatically. The method executes the template with the provided data
// and writes the output to the message body. If the template is nil or fails to execute, an error will
// be returned.
//
// Parameters:
// - tpl: A pointer to the text/template.Template containing a named template.
// - name: Name of the template to be used for the message body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) SetBodyNamedTextTemplate(tpl *tt.Template, name string, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.ExecuteTemplate(buffer, name, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextPlain, writeFunc, opts...)
return nil
}
// SetBodyHTMLTemplate sets the body of the message from a given html/template.Template pointer.
//
// This method sets the body of the message using the provided HTML template and data. The content type
// will be set to "text/html" automatically. The method executes the template with the provided data
// and writes the output to the message body. If the template is nil or fails to execute, an error will
// be returned.
//
// Parameters:
// - tpl: A pointer to the html/template.Template to be used for the message body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) SetBodyHTMLTemplate(tpl *ht.Template, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
// SetBodyNamedHTMLTemplate sets the body of the message from a template associated with a given
// html/template.Template pointer that has the given name.
//
// This method sets the body of the message using the provided HTML template, name and data. The content type
// will be set to "text/html" automatically. The method executes the template with the provided data
// and writes the output to the message body. If the template is nil or fails to execute, an error will
// be returned.
//
// Parameters:
// - tpl: A pointer to the html/template.Template containing a named template.
// - name: Name of the template to be used for the message body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) SetBodyNamedHTMLTemplate(tpl *ht.Template, name string, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.ExecuteTemplate(buffer, name, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.SetBodyWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
// AddAlternativeTextTemplate sets the alternative body of the message to a text/template.Template output.
//
// The content type will be set to "text/plain" automatically. This method executes the provided text template
// with the given data and adds the result as an alternative version of the message body. If the template
// is nil or fails to execute, an error will be returned.
//
// Parameters:
// - tpl: A pointer to the text/template.Template to be used for the alternative body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the alternative body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) AddAlternativeTextTemplate(tpl *tt.Template, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextPlain, writeFunc, opts...)
return nil
}
// AddAlternativeNamedTextTemplate sets the body of the message from a template associated with a given
// text/template.Template pointer that has the given name.
//
// The content type will be set to "text/plain" automatically. This method executes the named template provided by text template
// with the given data and adds the result as an alternative version of the message body. If the template
// is nil or fails to execute, an error will be returned.
//
// Parameters:
// - tpl: A pointer to the text/template.Template containing a named template.
// - name: Name of the template to be used for the alternative body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the alternative body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) AddAlternativeNamedTextTemplate(tpl *tt.Template, name string, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.ExecuteTemplate(buffer, name, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextPlain, writeFunc, opts...)
return nil
}
// AddAlternativeHTMLTemplate sets the alternative body of the message to an html/template.Template output.
//
// The content type will be set to "text/html" automatically. This method executes the provided HTML template
// with the given data and adds the result as an alternative version of the message body. If the template
// is nil or fails to execute, an error will be returned.
//
// Parameters:
// - tpl: A pointer to the html/template.Template to be used for the alternative body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the alternative body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) AddAlternativeHTMLTemplate(tpl *ht.Template, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.Execute(buffer, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
// AddAlternativeNamedHTMLTemplate sets the body of the message from a template associated with a given
// html/template.Template pointer that has the given name.
//
// The content type will be set to "html/plain" automatically. This method executes the named template provided by text template
// with the given data and adds the result as an alternative version of the message body. If the template
// is nil or fails to execute, an error will be returned.
//
// Parameters:
// - tpl: A pointer to the html/template.Template containing a named template.
// - name: Name of the template to be used for the alternative body.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the alternative body part.
//
// Returns:
// - An error if the template is nil or fails to execute, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2045
// - https://datatracker.ietf.org/doc/html/rfc2046
func (m *Msg) AddAlternativeNamedHTMLTemplate(tpl *ht.Template, name string, data any, opts ...PartOption) error {
if tpl == nil {
return errors.New(errTplPointerNil)
}
buffer := bytes.NewBuffer(nil)
if err := tpl.ExecuteTemplate(buffer, name, data); err != nil {
return fmt.Errorf(errTplExecuteFailed, err)
}
writeFunc := writeFuncFromBuffer(buffer)
m.AddAlternativeWriter(TypeTextHTML, writeFunc, opts...)
return nil
}
// AttachTextTemplate adds the output of a text/template.Template pointer as a File attachment to the Msg.
//
// This method allows you to attach the rendered output of a text template as a file to the message.
// The template is executed with the provided data, and its output is attached as a file. If the template
// fails to execute, an error will be returned.
//
// Parameters:
// - name: The name of the file to be attached.
// - tpl: A pointer to the text/template.Template to be executed for the attachment.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the attachment.
//
// Returns:
// - An error if the template fails to execute or cannot be attached, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) AttachTextTemplate(
name string, tpl *tt.Template, data any, opts ...FileOption,
) error {
file, err := fileFromTextTemplate(name, tpl, data)
if err != nil {
return fmt.Errorf("failed to attach template: %w", err)
}
m.attachments = m.appendFile(m.attachments, file, opts...)
return nil
}
// AttachNamedTextTemplate adds the output of a template associated with the text/template.Template pointer as a File attachment to the Msg.
//
// This method allows you to attach the rendered output of a text template as a file to the message.
// The named template associated with the provided text/template is executed with the provided data, and its output is attached as a file.
// If the template fails to execute, an error will be returned.
//
// Parameters:
// - fileName: The name of the file to be attached.
// - tpl: A pointer to the text/template.Template associated with the template to be executed for the attachment.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the attachment.
//
// Returns:
// - An error if the template fails to execute or cannot be attached, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) AttachNamedTextTemplate(
fileName string, tpl *tt.Template, tplName string, data any, opts ...FileOption,
) error {
file, err := fileFromNamedTextTemplate(fileName, tpl, tplName, data)
if err != nil {
return fmt.Errorf("failed to attach template: %w", err)
}
m.attachments = m.appendFile(m.attachments, file, opts...)
return nil
}
// AttachHTMLTemplate adds the output of a html/template.Template pointer as a File attachment to the Msg.
//
// This method allows you to attach the rendered output of an HTML template as a file to the message.
// The template is executed with the provided data, and its output is attached as a file. If the template
// fails to execute, an error will be returned.
//
// Parameters:
// - name: The name of the file to be attached.
// - tpl: A pointer to the html/template.Template to be executed for the attachment.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the attachment.
//
// Returns:
// - An error if the template fails to execute or cannot be attached, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) AttachHTMLTemplate(
name string, tpl *ht.Template, data any, opts ...FileOption,
) error {
file, err := fileFromHTMLTemplate(name, tpl, data)
if err != nil {
return fmt.Errorf("failed to attach template: %w", err)
}
m.attachments = m.appendFile(m.attachments, file, opts...)
return nil
}
// AttachNamedHTMLTemplate adds the output of a template associated with the html/template.Template pointer as a File attachment to the Msg.
//
// This method allows you to attach the rendered output of a html template as a file to the message.
// The named template associated with the provided html/template is executed with the provided data, and its output is attached as a file.
// If the template fails to execute, an error will be returned.
//
// Parameters:
// - fileName: The name of the file to be attached.
// - tpl: A pointer to the html/template.Template associated with the template to be executed for the attachment.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the attachment.
//
// Returns:
// - An error if the template fails to execute or cannot be attached, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) AttachNamedHTMLTemplate(
fileName string, tpl *ht.Template, tplName string, data any, opts ...FileOption,
) error {
file, err := fileFromNamedHTMLTemplate(fileName, tpl, tplName, data)
if err != nil {
return fmt.Errorf("failed to attach template: %w", err)
}
m.attachments = m.appendFile(m.attachments, file, opts...)
return nil
}
// EmbedTextTemplate adds the output of a text/template.Template pointer as an embedded File to the Msg.
//
// This method embeds the rendered output of a text template into the email message. The template is
// executed with the provided data, and its output is embedded as a file. If the template fails to execute,
// an error will be returned.
//
// Parameters:
// - name: The name of the embedded file.
// - tpl: A pointer to the text/template.Template to be executed for the embedded content.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the embedded file.
//
// Returns:
// - An error if the template fails to execute or cannot be embedded, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) EmbedTextTemplate(
name string, tpl *tt.Template, data any, opts ...FileOption,
) error {
file, err := fileFromTextTemplate(name, tpl, data)
if err != nil {
return fmt.Errorf("failed to embed template: %w", err)
}
m.embeds = m.appendFile(m.embeds, file, opts...)
return nil
}
// EmbedNamedTextTemplate adds the output of a template associated with the text/template.Template pointer as an embedded File to the Msg.
//
// This method embeds the rendered output of a text template into the email message. The named template associated with the provided text/template is
// executed with the provided data, and its output is embedded as a file. If the template fails to execute,
// an error will be returned.
//
// Parameters:
// - fileName: The name of the embedded file.
// - tpl: A pointer to the text/template.Template associated with the template to be executed for the embedded content.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the embedded file.
//
// Returns:
// - An error if the template fails to execute or cannot be embedded, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) EmbedNamedTextTemplate(
fileName string, tpl *tt.Template, tplName string, data any, opts ...FileOption,
) error {
file, err := fileFromNamedTextTemplate(fileName, tpl, tplName, data)
if err != nil {
return fmt.Errorf("failed to embed template: %w", err)
}
m.embeds = m.appendFile(m.embeds, file, opts...)
return nil
}
// EmbedHTMLTemplate adds the output of a html/template.Template pointer as an embedded File to the Msg.
//
// This method embeds the rendered output of an HTML template into the email message. The template is
// executed with the provided data, and its output is embedded as a file. If the template fails to execute,
// an error will be returned.
//
// Parameters:
// - name: The name of the embedded file.
// - tpl: A pointer to the html/template.Template to be executed for the embedded content.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the embedded file.
//
// Returns:
// - An error if the template fails to execute or cannot be embedded, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) EmbedHTMLTemplate(
name string, tpl *ht.Template, data any, opts ...FileOption,
) error {
file, err := fileFromHTMLTemplate(name, tpl, data)
if err != nil {
return fmt.Errorf("failed to embed template: %w", err)
}
m.embeds = m.appendFile(m.embeds, file, opts...)
return nil
}
// EmbedNamedHTMLTemplate adds the output of a template associated with the html/template.Template pointer as an embedded File to the Msg.
//
// This method embeds the rendered output of a html template into the email message. The named template associated with the provided html/template is
// executed with the provided data, and its output is embedded as a file. If the template fails to execute,
// an error will be returned.
//
// Parameters:
// - fileName: The name of the embedded file.
// - tpl: A pointer to the html/template.Template associated with the template to be executed for the embedded content.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
// - opts: Optional parameters for customizing the embedded file.
//
// Returns:
// - An error if the template fails to execute or cannot be embedded, otherwise nil.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func (m *Msg) EmbedNamedHTMLTemplate(
fileName string, tpl *ht.Template, tplName string, data any, opts ...FileOption,
) error {
file, err := fileFromNamedHTMLTemplate(fileName, tpl, tplName, data)
if err != nil {
return fmt.Errorf("failed to embed template: %w", err)
}
m.embeds = m.appendFile(m.embeds, file, opts...)
return nil
}
// fileFromTextTemplate returns a File pointer from a given text/template.Template.
//
// This method executes the provided text template with the given data and creates a File structure
// representing the output. The rendered template content is stored in a buffer and then processed
// as a file attachment or embed.
//
// Parameters:
// - name: The name of the file to be created from the template output.
// - tpl: A pointer to the text/template.Template to be executed.
// - data: The data to populate the template.
//
// Returns:
// - A pointer to the File structure representing the rendered template.
// - An error if the template is nil or if it fails to execute.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func fileFromTextTemplate(name string, tpl *tt.Template, data any) (*File, error) {
if tpl == nil {
return nil, errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.Execute(&buffer, data); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
return fileFromReader(name, &buffer)
}
// fileFromNamedTextTemplate returns a File pointer from a named template associated with a given text/template.Template.
//
// This method executes the named template assosociated with the provided text template with the given data and creates a File structure
// representing the output. The rendered template content is stored in a buffer and then processed
// as a file attachment or embed.
//
// Parameters:
// - fileName: The name of the file to be created from the template output.
// - tpl: A pointer to the text/template.Template associated with the template to be executed.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
//
// Returns:
// - A pointer to the File structure representing the rendered template.
// - An error if the template is nil or if it fails to execute.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func fileFromNamedTextTemplate(fileName string, tpl *tt.Template, tplName string, data any) (*File, error) {
if tpl == nil {
return nil, errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.ExecuteTemplate(&buffer, tplName, data); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
return fileFromReader(fileName, &buffer)
}
// fileFromHTMLTemplate returns a File pointer from a given html/template.Template.
//
// This method executes the provided HTML template with the given data and creates a File structure
// representing the output. The rendered template content is stored in a buffer and then processed
// as a file attachment or embed.
//
// Parameters:
// - name: The name of the file to be created from the template output.
// - tpl: A pointer to the html/template.Template to be executed.
// - data: The data to populate the template.
//
// Returns:
// - A pointer to the File structure representing the rendered template.
// - An error if the template is nil or if it fails to execute.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func fileFromHTMLTemplate(name string, tpl *ht.Template, data any) (*File, error) {
if tpl == nil {
return nil, errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.Execute(&buffer, data); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
return fileFromReader(name, &buffer)
}
// fileFromNamedHTMLTemplate returns a File pointer from a named template associated with a given html/template.Template.
//
// This method executes the named template assosociated with the provided html template with the given data and creates a File structure
// representing the output. The rendered template content is stored in a buffer and then processed
// as a file attachment or embed.
//
// Parameters:
// - fileName: The name of the file to be created from the template output.
// - tpl: A pointer to the html/template.Template associated with the template to be executed.
// - tplName: A name of the template from tpl to be executed.
// - data: The data to populate the template.
//
// Returns:
// - A pointer to the File structure representing the rendered template.
// - An error if the template is nil or if it fails to execute.
//
// References:
// - https://datatracker.ietf.org/doc/html/rfc2183
func fileFromNamedHTMLTemplate(fileName string, tpl *ht.Template, tplName string, data any) (*File, error) {
if tpl == nil {
return nil, errors.New(errTplPointerNil)
}
buffer := bytes.Buffer{}
if err := tpl.ExecuteTemplate(&buffer, tplName, data); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
return fileFromReader(fileName, &buffer)
}