-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
55 lines (44 loc) · 1.22 KB
/
email.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
49
50
51
52
53
54
55
package main
import (
"log"
"net/mail"
"github.com/domodwyer/mailyak"
)
// Email represents a single email message
type Email struct {
From string `json:"from"`
To string `json:"to"`
Cc []string `json:"cc"`
Bcc []string `json:"bcc"`
Subject string `json:"subject"`
TextBody string `json:"text_body"`
HTMLBody string `json:"html_body"`
}
// Send sends the instance of Email using the given instance of mailyak.MailYak.
// It expects the instance of mailyak.MailYak to have been set up previously
// with a valid hostname and implementer of smtp.Auth, such as smtp.PlainAuth.
func (e *Email) Send(yak *mailyak.MailYak) error {
from, err := mail.ParseAddress(e.From)
if err != nil {
log.Fatal(err)
}
to, err := mail.ParseAddress(e.To)
if err != nil {
log.Fatal(err)
}
// Decompress email text/html content
textBody, htmlBody := decompressBody(e)
yak.To(to.Address)
yak.From(from.Address)
yak.Subject(e.Subject)
yak.HTML().Set(htmlBody)
yak.Plain().Set(textBody)
if err := yak.Send(); err != nil {
return err
}
logger.Printf("info: Sent email to %s", e.To)
return nil
}
func decompressBody(e *Email) (string, string) {
return Decompress(e.TextBody), Decompress(e.HTMLBody)
}