|
1 | 1 | package mailer
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 | 6 | "net/http"
|
6 | 7 | "net/url"
|
@@ -39,34 +40,81 @@ type EmailData struct {
|
39 | 40 |
|
40 | 41 | // NewMailer returns a new gotrue mailer
|
41 | 42 | func NewMailer(globalConfig *conf.GlobalConfiguration) Mailer {
|
42 |
| - from := globalConfig.SMTP.FromAddress() |
43 |
| - u, _ := url.ParseRequestURI(globalConfig.API.ExternalURL) |
| 43 | + mailClient := NewMailClient(globalConfig) |
| 44 | + return &TemplateMailer{ |
| 45 | + SiteURL: globalConfig.SiteURL, |
| 46 | + Config: globalConfig, |
| 47 | + MailClient: mailClient, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +type emailValidatorMailClient struct { |
| 52 | + ev *EmailValidator |
| 53 | + mc MailClient |
| 54 | +} |
| 55 | + |
| 56 | +// Mail implements mailer.MailClient interface by calling validate before |
| 57 | +// passing the mail request to the next MailClient. |
| 58 | +func (o *emailValidatorMailClient) Mail( |
| 59 | + ctx context.Context, |
| 60 | + to string, |
| 61 | + subjectTemplate string, |
| 62 | + templateURL string, |
| 63 | + defaultTemplate string, |
| 64 | + templateData map[string]any, |
| 65 | + headers map[string][]string, |
| 66 | + typ string, |
| 67 | +) error { |
| 68 | + if err := o.ev.Validate(ctx, to); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + return o.mc.Mail( |
| 72 | + ctx, |
| 73 | + to, |
| 74 | + subjectTemplate, |
| 75 | + templateURL, |
| 76 | + defaultTemplate, |
| 77 | + templateData, |
| 78 | + headers, |
| 79 | + typ, |
| 80 | + ) |
| 81 | +} |
44 | 82 |
|
45 |
| - var mailClient MailClient |
| 83 | +// NewMailerWithClient returns a new Mailer that will use the given MailClient. |
| 84 | +func NewMailerWithClient( |
| 85 | + globalConfig *conf.GlobalConfiguration, |
| 86 | + mailClient MailClient, |
| 87 | +) Mailer { |
| 88 | + ev := newEmailValidator(globalConfig.Mailer) |
| 89 | + mr := &emailValidatorMailClient{ev: ev, mc: mailClient} |
| 90 | + return &TemplateMailer{ |
| 91 | + SiteURL: globalConfig.SiteURL, |
| 92 | + Config: globalConfig, |
| 93 | + MailClient: mr, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// NewMailClient returns a new MailClient based on the given configuration. |
| 98 | +func NewMailClient(globalConfig *conf.GlobalConfiguration) MailClient { |
46 | 99 | if globalConfig.SMTP.Host == "" {
|
47 | 100 | logrus.Infof("Noop mail client being used for %v", globalConfig.SiteURL)
|
48 |
| - mailClient = &noopMailClient{ |
49 |
| - EmailValidator: newEmailValidator(globalConfig.Mailer), |
50 |
| - } |
51 |
| - } else { |
52 |
| - mailClient = &MailmeMailer{ |
53 |
| - Host: globalConfig.SMTP.Host, |
54 |
| - Port: globalConfig.SMTP.Port, |
55 |
| - User: globalConfig.SMTP.User, |
56 |
| - Pass: globalConfig.SMTP.Pass, |
57 |
| - LocalName: u.Hostname(), |
58 |
| - From: from, |
59 |
| - BaseURL: globalConfig.SiteURL, |
60 |
| - Logger: logrus.StandardLogger(), |
61 |
| - MailLogging: globalConfig.SMTP.LoggingEnabled, |
| 101 | + return &noopMailClient{ |
62 | 102 | EmailValidator: newEmailValidator(globalConfig.Mailer),
|
63 | 103 | }
|
64 | 104 | }
|
65 | 105 |
|
66 |
| - return &TemplateMailer{ |
67 |
| - SiteURL: globalConfig.SiteURL, |
68 |
| - Config: globalConfig, |
69 |
| - Mailer: mailClient, |
| 106 | + from := globalConfig.SMTP.FromAddress() |
| 107 | + u, _ := url.ParseRequestURI(globalConfig.API.ExternalURL) |
| 108 | + return &MailmeMailer{ |
| 109 | + Host: globalConfig.SMTP.Host, |
| 110 | + Port: globalConfig.SMTP.Port, |
| 111 | + User: globalConfig.SMTP.User, |
| 112 | + Pass: globalConfig.SMTP.Pass, |
| 113 | + LocalName: u.Hostname(), |
| 114 | + From: from, |
| 115 | + BaseURL: globalConfig.SiteURL, |
| 116 | + Logger: logrus.StandardLogger(), |
| 117 | + MailLogging: globalConfig.SMTP.LoggingEnabled, |
70 | 118 | }
|
71 | 119 | }
|
72 | 120 |
|
|
0 commit comments