add from config
This commit is contained in:
parent
9836b30191
commit
e47d3abbef
21
mailer.go
21
mailer.go
|
@ -16,6 +16,7 @@ type Mailer struct {
|
|||
}
|
||||
|
||||
type SMTPConfig struct {
|
||||
From string
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
|
@ -29,6 +30,7 @@ func NewMailer(templates_dir string, tplcfg *viper.Viper, smtpconfig *viper.Vipe
|
|||
TemplatesDir: templates_dir,
|
||||
TemplatesConfig: tplcfg,
|
||||
SMTPConfig: SMTPConfig{
|
||||
From: smtpconfig.GetString("from"),
|
||||
Host: smtpconfig.GetString("host"),
|
||||
Port: smtpconfig.GetInt("port"),
|
||||
Username: smtpconfig.GetString("username"),
|
||||
|
@ -47,7 +49,7 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro
|
|||
}
|
||||
t := template.New("email").Funcs(
|
||||
template.FuncMap{
|
||||
"unescapeHTML": UnescapeHTML,
|
||||
"unescapeHTML": UnescapeHTML,
|
||||
},
|
||||
)
|
||||
t = template.Must(t.ParseFiles(prefixed_files...))
|
||||
|
@ -58,6 +60,11 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro
|
|||
body := buf.String()
|
||||
|
||||
message := mail.NewMsg()
|
||||
fromAddress := m.SMTPConfig.From
|
||||
if fromAddress == "" {
|
||||
fromAddress = m.SMTPConfig.Username
|
||||
}
|
||||
|
||||
if err := message.From(m.SMTPConfig.Username); err != nil {
|
||||
return fmt.Errorf("failed to set From header : %w", err)
|
||||
}
|
||||
|
@ -68,21 +75,21 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro
|
|||
message.SetBodyString(mail.TypeTextHTML, body)
|
||||
|
||||
dialOptions := []mail.Option{
|
||||
//mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthNoAuth),
|
||||
mail.WithUsername(m.SMTPConfig.Username),
|
||||
mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover),
|
||||
// mail.WithSMTPAuth(mail.SMTPAuthNoAuth),
|
||||
mail.WithUsername(m.SMTPConfig.Username),
|
||||
mail.WithPassword(m.SMTPConfig.Password),
|
||||
}
|
||||
|
||||
fmt.Println(dialOptions)
|
||||
|
||||
for _, opt := range opts {
|
||||
no, err := opt(message, dialOptions)
|
||||
no, err := opt(message, dialOptions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set option : %w", err)
|
||||
}
|
||||
dialOptions = no
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(dialOptions)
|
||||
|
||||
|
@ -96,7 +103,6 @@ func (m *Mailer) Send(emailcfg string, to string, data any, opts ...Option) erro
|
|||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func WithReplyTo(email string) Option {
|
||||
|
@ -115,4 +121,3 @@ func WithTLSOpportunistic() Option {
|
|||
func UnescapeHTML(s string) template.HTML {
|
||||
return template.HTML(s)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue