-
Notifications
You must be signed in to change notification settings - Fork 15
/
writer_mailer.go
48 lines (40 loc) · 1.2 KB
/
writer_mailer.go
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
package emailservices
import (
"io"
"text/template"
"github.com/ashirt-ops/ashirt-server/backend/logging"
)
// WriterMailer acts as a no-email email server for monitoring when running locally. All emails are
// printed to the provided writer. This is intended to be used with os.Stdout, but any writer should
// work
type WriterMailer struct {
writer io.Writer
logger logging.Logger
}
// MakeWriterMailer constructs a WriterMailer
func MakeWriterMailer(w io.Writer, logger logging.Logger) WriterMailer {
return WriterMailer{
writer: w,
logger: logger,
}
}
// AddToQueue writes the email to the writer provided in MakeWriterMailer
// returns an error if the underlying template cannot be executed
func (m *WriterMailer) AddToQueue(job EmailJob) error {
err := m.yellEmail(job)
job.OnCompleted(err)
return err
}
func (m *WriterMailer) yellEmail(job EmailJob) error {
return emailPrintFormat.Execute(m.writer, job)
}
var emailPrintFormat = template.Must(template.New("yell-email").Parse(
"\n" +
`======================================================
To: | {{ .To }}
From: | {{ .From }}
Subject: | {{ .Subject }}
Body:
{{ .Body }}
` +
"======================================================\n"))