scron/smtp.go
kekskurse dc28110856
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
feat: smtp notification
2025-07-31 20:11:30 +02:00

38 lines
850 B
Go

package main
import (
"fmt"
"github.com/rs/zerolog/log"
"gopkg.in/mail.v2"
)
func SendSMTPNotification(sc smtpConfig, title, message string) error {
// Validate required fields
if sc.Host == "" || sc.Port == 0 || sc.From == "" || sc.To == "" {
return fmt.Errorf("incomplete SMTP configuration")
}
// Create new message
m := mail.NewMessage()
m.SetHeader("From", sc.From)
// Split and trim recipients
m.SetHeader("To", sc.To)
m.SetHeader("Subject", title)
m.SetBody("text/plain", message)
// Create SMTP dialer
d := mail.NewDialer(sc.Host, sc.Port, sc.Username, sc.Password)
// Configure TLS
d.SSL = sc.UseSSL
// Send the email
if err := d.DialAndSend(m); err != nil {
return fmt.Errorf("failed to send email: %w", err)
}
log.Info().Str("subject", title).Str("to", sc.To).Msg("Email notification sent")
return nil
}