GO 发送邮件,官方连包都帮我们写好了,真是贴心啊!!!,突然间感觉php不香了。。。
package main
import (
"fmt"
"net/smtp"
"strings"
)
func SendToMail(user,sendUserName,password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + sendUserName+"<"+user+">" + "\r\nSubject: " +subject+ "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func main() {
user := "admin@buruyouni.com"
password := "**你的密码**"
host := "smtpdm.aliyun.com:80"
to := "269754243@qq.com"
subject := "使用Golang发送邮件"
body := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="iso-8859-15">
<title>MMOGA POWER</title>
</head>
<body>
GO 发送邮件,官方连包都帮我们写好了,真是贴心啊!!!
</body>
</html>`
sendUserName := "GOLANG SEND MAIL"//发送邮件的人名称
fmt.Println("send email")
err := SendToMail(user,sendUserName,password, host, to, subject, body, "html")
if err != nil {
fmt.Println("Send mail error!")
fmt.Println(err)
} else {
fmt.Println("Send mail success!")
}
}
没错,官方连smtp包都写好了