sendmail/mail.go

46 lines
884 B
Go
Raw Normal View History

package main
import (
"os"
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
}
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)
}
}