Skip to content

Instantly share code, notes, and snippets.

@JesseYan
Created August 8, 2019 10:16
Show Gist options
  • Save JesseYan/4b0bb3c1aeaf6f8ce3c7688a34d31e11 to your computer and use it in GitHub Desktop.
Save JesseYan/4b0bb3c1aeaf6f8ce3c7688a34d31e11 to your computer and use it in GitHub Desktop.
go send email
//SendEmail 发送邮件
func SendEmail(conf *EmailConfig) error {
m := gomail.NewMessage()
m.SetHeader("From", conf.From)
m.SetHeader("To", conf.To...)
m.SetHeader("Subject", conf.Subject)
if conf.ContentType == EmailTextType {
m.SetBody("text/plain", conf.ContentBuffer.String())
} else if conf.ContentType == EmailHTMLType {
m.SetBody("text/html", conf.ContentBuffer.String())
} else {
return errors.Errorf("unsupported Email ContentType, it should be text or html but it is %d", conf.ContentType)
}
go func() {
d := gomail.Dialer{Host: conf.Host, Port: conf.Port}
if err := d.DialAndSend(m); err != nil {
log.Logger.Errorf("Send Email error, Error info [%s], EmailConfig host[%s], Port[%d] ContentType[%d]",
err.Error(), conf.Host, conf.Port, conf.ContentType)
}
}()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment