forked from stanislas-m/mocksmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevsmtp.go
More file actions
42 lines (34 loc) · 1 KB
/
devsmtp.go
File metadata and controls
42 lines (34 loc) · 1 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
package devsmtp
import (
"io/ioutil"
"strings"
"github.com/ernsheong/grand"
"github.com/gobuffalo/buffalo/mail"
"github.com/pkg/browser"
)
// DevSMTP is an in-memory implementation for buffalo `Sender`
// interface. It's intended to open sent messages in the browser for debugging purposes.
type DevSMTP struct {
}
// Send implements buffalo `Sender` interface, to open the mail in the browser.
func (s *DevSMTP) Send(m mail.Message) error {
filehash := grand.GenerateRandomString(16)
previewFilename := "tmp/preview_" + filehash + ".html"
// Only care about HTML for now
htmlContent := ""
for _, body := range m.Bodies {
// handle both "text/html" and "text/html; charset=utf-8"
if strings.HasPrefix(body.ContentType, "text/html") {
htmlContent += body.Content
}
}
if err := ioutil.WriteFile(previewFilename, []byte(htmlContent), 0644); err != nil {
return err
}
defer browser.OpenFile(previewFilename)
return nil
}
// New constructs a new DevSMTP.
func New() *DevSMTP {
return &DevSMTP{}
}