-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
90 lines (79 loc) · 1.76 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
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
package iemail
import (
"github.com/matcornic/hermes/v2"
"gopkg.in/gomail.v2"
"net/mail"
)
type Config struct {
Host string
Port int
Username string
Password string
Product Product
}
type Product struct {
Name string
Link string
Logo string
Copyright string
TroubleText string
}
type Header struct {
From mail.Address
To string
Subject string
}
type EmailSend struct {
c *Config
}
func New(c *Config) *EmailSend {
e := &EmailSend{c: c}
return e
}
func (e *EmailSend) SendHTML(header Header, content hermes.Email) error {
typ := "text/html"
body, err := e.GenBody(typ, content)
if err != nil {
return err
}
return e.send(header, typ, body)
}
func (e *EmailSend) SendPlainText(header Header, content hermes.Email) error {
typ := "text/plain"
body, err := e.GenBody(typ, content)
if err != nil {
return err
}
return e.send(header, typ, body)
}
func (e *EmailSend) GenBody(typ string, content hermes.Email) (string, error) {
h := hermes.Hermes{
Theme: new(hermes.Default),
TextDirection: "",
Product: hermes.Product{
Name: e.c.Product.Name,
Link: e.c.Product.Link,
Logo: e.c.Product.Logo,
Copyright: e.c.Product.Copyright,
TroubleText: e.c.Product.TroubleText,
},
DisableCSSInlining: false,
}
switch typ {
case "text/html":
return h.GenerateHTML(content)
default:
return h.GeneratePlainText(content)
}
}
func (e *EmailSend) send(header Header, typ, body string) error {
msg := gomail.NewMessage()
msg.SetHeader("From", header.From.String())
msg.SetHeader("To", header.To)
msg.SetHeader("Subject", header.Subject)
if typ != "" {
msg.SetBody(typ, body)
}
d := gomail.NewDialer(e.c.Host, e.c.Port, e.c.Username, e.c.Password)
return d.DialAndSend(msg)
}