PS.参考博文(https://www.ctolib.com/meehow-sendmail.html)
1 概述
我们的主角正是sendmail库,相比smtp来说不需要自己额外搭建smtp server,授权码等,做到如PHP版发邮件。调用简单,它不仅可以与Sendmail一起使用,而且还可以与其他提供兼容性接口的MTA(例如Postfix或sSMTP)一起使用。
2 特点
- 将header与body分离
- 使用UTF-8编码
- 不需要任何SMTP配置
- 只需要宿主机器有/usr/sbin/sendmail命令,大多数系统均支持
- ...
3 安装
> go get -u github.com/meehow/sendmail
4 使用
package main
import (
"io"
"log"
"net/mail"
"github.com/meehow/sendmail"
)
func main() {
sm := sendmail.Mail{
Subject: "测试",
From: &mail.Address{"guoyimeng", “guoyimeng@360.cn"},
To: []*mail.Address{
{"guoyimeng", "guoyimeng@360.cn”},
{"guoyimeng1", "guoyimeng1@360.cn”},
},
}
io.WriteString(&sm.Text, ":)\r\n")
if err := sm.Send(); err != nil {
log.Println(err)
}
}
Instead of io.WriteString, you can execute a template:
tpl := template.Must(tsemplate.New("email").Parse(`Hello {{.Name}}!`))
tpl.ExecuteTemplate(&sm.Text, "email", &struct{ Name string }{"Dominik"})