-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.go
142 lines (113 loc) · 2.74 KB
/
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
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
package makeless_go_mailer_smtp
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/jordan-wright/email"
"github.com/makeless/makeless-go/v2/mail"
"github.com/makeless/makeless-go/v2/queue"
"github.com/makeless/makeless-go/v2/queue/basic"
"net/smtp"
"sync"
)
type Mailer struct {
Queue makeless_go_queue.Queue
Auth smtp.Auth
Tls *tls.Config
Host string
Port string
Identity string
Username string
Password string
*sync.RWMutex
}
func (mailer *Mailer) GetQueue() makeless_go_queue.Queue {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Queue
}
func (mailer *Mailer) GetAuth() smtp.Auth {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Auth
}
func (mailer *Mailer) SetAuth(auth smtp.Auth) {
mailer.Lock()
defer mailer.Unlock()
mailer.Auth = auth
}
func (mailer *Mailer) GetTls() *tls.Config {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Tls
}
func (mailer *Mailer) GetHost() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Host
}
func (mailer *Mailer) GetPort() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Port
}
func (mailer *Mailer) GetIdentity() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Identity
}
func (mailer *Mailer) GetUsername() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Username
}
func (mailer *Mailer) GetPassword() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Password
}
func (mailer *Mailer) Init() error {
mailer.SetAuth(smtp.PlainAuth(
mailer.GetIdentity(),
mailer.GetUsername(),
mailer.GetPassword(),
mailer.GetHost(),
))
return nil
}
func (mailer *Mailer) Send(ctx context.Context, mail makeless_go_mail.Mail) error {
var e = &email.Email{
To: mail.GetTo(),
Cc: mail.GetCc(),
Bcc: mail.GetBcc(),
From: mail.GetFrom(),
Subject: mail.GetSubject(),
Text: mail.GetMessage(),
HTML: mail.GetHtmlMessage(),
Attachments: nil,
Headers: mail.GetHeaders(),
}
for _, attachment := range mail.GetAttachments() {
e.Attachments = append(e.Attachments, &email.Attachment{
Filename: attachment.GetFilename(),
Header: attachment.GetHeaders(),
Content: attachment.GetData(),
HTMLRelated: false,
})
}
if mailer.Tls == nil {
return e.Send(fmt.Sprintf("%s:%s", mailer.GetHost(), mailer.GetPort()), mailer.GetAuth())
}
return e.SendWithTLS(fmt.Sprintf("%s:%s", mailer.GetHost(), mailer.GetPort()), mailer.GetAuth(), mailer.GetTls())
}
func (mailer *Mailer) SendQueue(mail makeless_go_mail.Mail) error {
bytes, err := json.Marshal(mail)
if err != nil {
return err
}
return mailer.GetQueue().Add(&makeless_go_queue_basic.Node{
Data: bytes,
RWMutex: new(sync.RWMutex),
})
}