|
1 | 1 | package providers |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "github.com/hirsch88/go-micro-framework/app/mail" |
6 | 5 | "github.com/hirsch88/go-micro-framework/config" |
7 | | - "github.com/hirsch88/go-micro-framework/core" |
8 | 6 | "go.uber.org/zap" |
9 | | - "html/template" |
10 | 7 | "log" |
11 | 8 | ) |
12 | 9 |
|
13 | 10 | type mailProvider struct { |
14 | | - config *config.MailConfig |
15 | | - log *zap.SugaredLogger |
| 11 | + config *config.MailConfig |
| 12 | + log *zap.SugaredLogger |
| 13 | + template TemplateProvider |
| 14 | + smtpMail SMTPMailProvider |
16 | 15 | } |
17 | 16 |
|
18 | | -func (p *mailProvider) Send(mail mail.Mailable, to string) { |
| 17 | +func (p *mailProvider) Send(mail mail.Mailable, to string) bool { |
19 | 18 | mailTemplate := mail.Build() |
20 | | - message, err := p.parseTemplate(mailTemplate) |
| 19 | + message, err := p.template.Parse(mailTemplate.TemplatePath, mailTemplate.Context) |
21 | 20 | if err != nil { |
22 | 21 | log.Fatal(err) |
23 | 22 | } |
24 | | - if ok := p.sendMail(to, mailTemplate.Subject, message); ok { |
25 | | - p.log.Infof("Email has been sent to %s", to) |
26 | | - } else { |
27 | | - p.log.Infof("Failed to send the email to %s", to) |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -func (p *mailProvider) parseTemplate(mailTemplate *mail.Template) (string, error) { |
32 | | - t, err := template.ParseFiles(mailTemplate.TemplatePath) |
33 | | - if err != nil { |
34 | | - return "", err |
35 | | - } |
36 | | - buffer := new(bytes.Buffer) |
37 | | - if err = t.Execute(buffer, mailTemplate.Context); err != nil { |
38 | | - return "", err |
39 | | - } |
40 | | - return buffer.String(), nil |
41 | | -} |
42 | | - |
43 | | -func (p *mailProvider) sendMail(to string, subject string, message string) bool { |
44 | | - smtpServer := core.SmtpServer{ |
45 | | - Host: p.config.Host, |
46 | | - Port: p.config.Port, |
47 | | - } |
48 | | - |
49 | | - mail := core.Mail{ |
50 | | - From: p.config.From, |
51 | | - To: to, |
52 | | - Subject: subject, |
53 | | - Body: message, |
54 | | - } |
55 | 23 |
|
56 | | - if err := core.SendMail(smtpServer, mail, p.config.Password); err != nil { |
| 24 | + if err := p.smtpMail.Send( |
| 25 | + SmtpServer{ |
| 26 | + Host: p.config.Host, |
| 27 | + Port: p.config.Port, |
| 28 | + }, |
| 29 | + Mail{ |
| 30 | + From: p.config.From, |
| 31 | + To: to, |
| 32 | + Subject: mailTemplate.Subject, |
| 33 | + Body: message, |
| 34 | + }, |
| 35 | + p.config.Password, |
| 36 | + ); err != nil { |
57 | 37 | return false |
58 | 38 | } |
59 | 39 |
|
60 | 40 | return true |
61 | 41 | } |
62 | 42 |
|
63 | 43 | type MailProvider interface { |
64 | | - Send(mail mail.Mailable, to string) |
| 44 | + Send(mail mail.Mailable, to string) bool |
65 | 45 | } |
66 | 46 |
|
67 | | -func NewMailProvider(config *config.MailConfig, log *zap.SugaredLogger) MailProvider { |
68 | | - return &mailProvider{config, log} |
| 47 | +func NewMailProvider(config *config.MailConfig, log *zap.SugaredLogger, template TemplateProvider, smtpMail SMTPMailProvider) MailProvider { |
| 48 | + return &mailProvider{config, log, template, smtpMail} |
69 | 49 | } |
0 commit comments