2024-10-26 15:01:40 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
2024-11-13 14:18:14 +00:00
|
|
|
"github.com/google/uuid"
|
2024-10-26 15:01:40 +00:00
|
|
|
"github.com/jordan-wright/email"
|
2024-11-13 14:18:14 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-10-26 15:01:40 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"net/smtp"
|
|
|
|
"net/textproto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SendMail(target string, in pushkit.EmailData) error {
|
2024-11-13 14:18:14 +00:00
|
|
|
requestId := uuid.NewString()
|
|
|
|
log.Debug().
|
|
|
|
Str("request_id", requestId).
|
|
|
|
Str("to", target).
|
|
|
|
Str("subject", in.Subject).
|
|
|
|
Msg("Sending email...")
|
|
|
|
|
2024-10-26 15:01:40 +00:00
|
|
|
mail := &email.Email{
|
|
|
|
To: []string{target},
|
|
|
|
From: viper.GetString("mailer.name"),
|
|
|
|
Subject: in.Subject,
|
|
|
|
Headers: textproto.MIMEHeader{},
|
|
|
|
}
|
|
|
|
if in.Text != nil {
|
|
|
|
mail.Text = []byte(*in.Text)
|
|
|
|
}
|
|
|
|
if in.HTML != nil {
|
|
|
|
mail.HTML = []byte(*in.HTML)
|
|
|
|
}
|
2024-11-13 14:18:14 +00:00
|
|
|
err := mail.SendWithTLS(
|
2024-10-26 15:01:40 +00:00
|
|
|
fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")),
|
|
|
|
smtp.PlainAuth(
|
|
|
|
"",
|
|
|
|
viper.GetString("mailer.username"),
|
|
|
|
viper.GetString("mailer.password"),
|
|
|
|
viper.GetString("mailer.smtp_host"),
|
|
|
|
),
|
|
|
|
&tls.Config{ServerName: viper.GetString("mailer.smtp_host")},
|
|
|
|
)
|
2024-11-13 14:18:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().
|
|
|
|
Str("request_id", requestId).
|
|
|
|
Str("to", target).
|
|
|
|
Str("subject", in.Subject).
|
|
|
|
Err(err).Msg("Failed to send email...")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2024-10-26 15:01:40 +00:00
|
|
|
}
|