chore: sendmail helper uses configuration

This commit is contained in:
Felipe M 2024-11-24 12:08:46 +01:00
parent c829408095
commit f96a7a5d7c
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8

View file

@ -1,11 +1,16 @@
package main
import (
"fmt"
"log"
"log/slog"
"net/smtp"
"os"
"git.nakama.town/fmartingr/gotoolkit/encoding"
"github.com/emersion/go-sasl"
"git.nakama.town/fmartingr/smtp2shoutrrr"
)
// The ANONYMOUS mechanism name.
@ -59,14 +64,44 @@ func NewPlainClient(identity, username, password string) smtp.Auth {
}
func main() {
var config smtp2shoutrrr.Config
configPath := "config.toml"
f, err := os.Open(configPath)
if err != nil {
slog.Error("Error opening config file", slog.String("err", err.Error()), slog.String("path", configPath))
return
}
enc := encoding.NewTOMLEncoding()
if err := enc.DecodeReader(f, &config); err != nil {
slog.Error("Error decoding config file", slog.String("err", err.Error()), slog.String("path", configPath))
return
}
config.SetDefaults()
// hostname is used by PlainAuth to validate the TLS certificate.
hostname := "localhost"
auth := NewPlainClient("", "username", "password")
auth := NewPlainClient("", config.Username, config.Password)
// auth := NewAnonymousClient("test")
recipients := []string{"something@something.com", "caca@caca.com"}
msg := []byte("\r\nwithout title")
slog.Info("Using first recipient configuration to send a test email")
if len(config.Recipients) == 0 {
slog.Error("No recipients found in configuration")
return
}
if len(config.Recipients[0].Addresses) == 0 {
slog.Error("No email addresses found in first recipient configuration")
return
}
recipients := []string{config.Recipients[0].Addresses[0]}
msg := []byte("Subject: Test notification\r\n\r\nThis is a test notification")
from := "hello@localhost"
err := smtp.SendMail(hostname+":11025", auth, from, recipients, msg)
err = smtp.SendMail(fmt.Sprintf("%s:%d", hostname, config.Port), auth, from, recipients, msg)
if err != nil {
log.Fatal(err)
}