Pusher/pkg/internal/provider/mailer.go

56 lines
1.2 KiB
Go
Raw Normal View History

2024-10-26 15:01:40 +00:00
package provider
import (
"crypto/tls"
"fmt"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/google/uuid"
2024-10-26 15:01:40 +00:00
"github.com/jordan-wright/email"
"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 {
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)
}
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")},
)
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
}