GO 发送邮件,官方连包都帮我们写好了

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包都写好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值