38 lines
850 B
Go
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
|
|
}
|