Если не принципиально использование smtp пакета, то я делал вот так:
import (
// ...
gomail "gopkg.in/gomail.v2"
)
func makeMailer() (*gomail.Dialer, error) {
smtp := "SMTP_MAIL_ADDRESS"
port, err := strconv.Atoi("PORT_NUM")
if err != nil {
fmt.Println("error parsing smtp port.", err)
return nil, err
}
name := "MAILER_AUTH_NAME"
pwd := "MAILER_AUTH_PWD"
if port == 0 || smtp == "" && name == "" {
return nil, errors.New("invalid mailer parameters")
}
return gomail.NewDialer(smtp, port, name, pwd), nil
}
func send() (*..., error) {
mailer, err := makeMailer()
if err != nil {
return nil, fmt.Errorf("can't create mailer. %v", err)
}
recipient := "MAILER_RECIPIENT"
if recipient == "" {
return nil, fmt.Errorf("empty recipient")
}
m := gomail.NewMessage()
m.SetHeader("From", "MAILER_AUTH_NAME")
m.SetHeader("To", "MAILER_RECIPIENT")
m.SetHeader("Subject", "MAILER_SUBJECT")
m.SetBody("text/plain", "MAIL_BODY_TEXT")
err = mailer.DialAndSend(m)
if err != nil {
return nil, fmt.Errorf("can't send digest. %v", err)
}
}