2020-02-10 21:48:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-04-28 23:44:51 +02:00
|
|
|
"time"
|
2020-02-10 21:48:41 +01:00
|
|
|
|
|
|
|
mail "gopkg.in/mail.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func formatMessage(config *Config) *mail.Message {
|
|
|
|
m := mail.NewMessage()
|
|
|
|
m.SetHeader("From", config.From)
|
|
|
|
m.SetHeader("To", config.To...)
|
|
|
|
if !IsEmpty(config.Cc) {
|
|
|
|
m.SetHeader("Cc", config.Cc...)
|
|
|
|
}
|
|
|
|
if !IsEmpty(config.Bcc) {
|
|
|
|
m.SetHeader("Bcc", config.Bcc...)
|
|
|
|
}
|
|
|
|
m.SetHeader("Subject", config.Subject)
|
|
|
|
m.SetBody("text/plain", config.Text)
|
|
|
|
Debug.F("Message to deliver:\n%s", m)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func deliverMessage(s *ServerConfig, m *mail.Message) error {
|
|
|
|
dialer := mail.NewDialer(
|
|
|
|
s.Address,
|
|
|
|
int(s.Port),
|
|
|
|
s.User,
|
|
|
|
s.Password,
|
|
|
|
)
|
|
|
|
if s.Encryption {
|
|
|
|
dialer.StartTLSPolicy = mail.MandatoryStartTLS
|
|
|
|
}
|
2020-04-28 23:44:51 +02:00
|
|
|
dialer.Timeout = 30 * time.Second
|
2020-02-10 21:48:41 +01:00
|
|
|
return dialer.DialAndSend(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendMail(config *Config) {
|
|
|
|
m := formatMessage(config)
|
|
|
|
if err := deliverMessage(config.Server, m); err != nil {
|
|
|
|
Error.F("Delivery failure:\n%s", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
}
|