48 lines
927 B
Go
48 lines
927 B
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
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
|
|
}
|
|
dialer.Timeout = 30 * time.Second
|
|
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)
|
|
}
|
|
}
|