tlm-login-server/internal/communication/mail.go

53 lines
1.5 KiB
Go

package communication
import (
"code.lila.network/lauralani/tlm-login-server/internal/config"
"code.lila.network/lauralani/tlm-login-server/internal/models"
"crypto/tls"
"github.com/go-mail/mail"
"log/slog"
"strings"
"text/template"
)
func SendRegistrationNotification(user models.User, registrationsecet string) error {
message := mail.NewMessage()
message.SetHeader("From", config.SMTPFrom)
message.SetHeader("To", user.Email)
message.SetHeader("Subject", "Your Registration for TLM Nation")
var data = registrationTemplateVars{
BaseUrl: config.BasePath,
RegistrationSecret: registrationsecet,
}
plainstring := new(strings.Builder)
texttemplate, err := template.New("registration.plain.tmpl").ParseFiles("internal/communication/templates/registration.plain.tmpl")
if err != nil {
slog.Error("Can't load template", "Template", "internal/communication/templates/registration.plain.tmpl")
return err
}
err = texttemplate.Execute(plainstring, data)
if err != nil {
slog.Error("Can't execute template", "Template", "internal/communication/templates/registration.plain.tmpl")
return err
}
message.SetBody("text/plain", plainstring.String())
slog.Debug("got requested to send mail:", "FROM", config.SMTPFrom, "TO", user.Email)
mailer := mail.NewDialer(config.SMTPServer, config.SMTPPort, config.SMTPUser, config.SMTPPassword)
if !config.SMTPVerifySSL {
mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
err = mailer.DialAndSend(message)
if err != nil {
return err
}
return nil
}