chore: sendmail helper uses configuration
This commit is contained in:
parent
c829408095
commit
f96a7a5d7c
1 changed files with 39 additions and 4 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue