-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.go
76 lines (64 loc) · 1.84 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
package email
import (
"crypto/tls"
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
"github.com/jordan-wright/email"
"net/smtp"
"time"
)
type Mailer struct {
name string
auth smtp.Auth
from string
address string
queue contracts.Queue
tlsConfig *tls.Config
}
func (this *Mailer) Raw(subject, text string, to []string) error {
newEmail := email.NewEmail()
newEmail.From = this.from
newEmail.To = to
newEmail.Subject = subject
newEmail.Text = []byte(text)
newEmail.HTML = []byte(text)
return newEmail.Send(this.address, this.auth)
}
func (this *Mailer) Send(mail contracts.Mailable) error {
if mail.GetQueue() != "" {
return this.Queue(mail, mail.GetQueue())
}
newEmail := email.NewEmail()
newEmail.From = mail.GetFrom()
if newEmail.From == "" {
newEmail.From = this.from
}
newEmail.To = mail.GetTo()
newEmail.Cc = mail.GetCc()
newEmail.Bcc = mail.GetBcc()
newEmail.Subject = mail.GetSubject()
newEmail.Text = []byte(mail.GetText())
newEmail.HTML = []byte(mail.GetHtml())
if this.tlsConfig != nil {
return newEmail.SendWithStartTLS(this.address, this.auth, this.tlsConfig)
}
return newEmail.Send(this.address, this.auth)
}
func (this *Mailer) Queue(mail contracts.Mailable, queue ...string) error {
if mail.GetDelay() > 0 {
return this.Later(mail.GetDelay(), mail, queue...)
}
return this.queue.Push(&Job{
UUID: fmt.Sprintf("email:%s-%s", utils.RandStr(10), mail.GetSubject()),
CreatedAt: time.Now().Unix(),
Mail: ConvertToMail(mail),
})
}
func (this *Mailer) Later(delay int, mail contracts.Mailable, queue ...string) error {
return this.queue.Later(time.Now().Add(time.Duration(delay)*time.Second), &Job{
UUID: fmt.Sprintf("email:%s-%s", utils.RandStr(10), mail.GetSubject()),
CreatedAt: time.Now().Unix(),
Mail: ConvertToMail(mail),
}, queue...)
}