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