[Go]解决go-smtp发送内容乱码和发送html邮件不解析
使用github.com/emersion/go-smtp , 通过smtp发送通知邮件内容
加上html代码的内容后 , 在一些邮箱里会被原样展示 , 并没有展示成html , 原因是没有加Content-Type , 加上之后就可以了
tools/smtp.go
package tools
import (
"encoding/base64"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"strings"
)
func SendSmtp(server string, from string, password string, to []string, subject string, body string) error {
auth := sasl.NewPlainClient("", from, password)
subjectBase := base64.StdEncoding.EncodeToString([]byte(subject))
msg := strings.NewReader(
"From: " + from + "
" +
"To: " + strings.Join(to, ",") + "
" +
"Subject: =?UTF-8?B?" + subjectBase + "?=
" +
"Content-Type: text/html; charset=UTF-8" +
"
" +
body + "
")
err := smtp.SendMail(server, auth, from, to, msg)
if err != nil {
return err
}
return nil
}

![[Go]解决go-smtp发送内容乱码和发送html邮件不解析](https://www.zixueka.com/wp-content/uploads/2024/01/1706707496-5871424113fc3d1.jpg)
