加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang发邮件

发布时间:2020-12-16 19:13:18 所属栏目:大数据 来源:网络整理
导读:由于go默认tls.Config{ServerName: host, InsecureSkipVerify : false }, func TestHtmlEmail(t *testing.T) {opt := EmailOptions{User: "",Password: "",To: []string{""},From: "",Addr: "smtp.xxx.com:25",}auth := NewLoginAuth(opt.User,opt.Password

由于go默认tls.Config{ServerName: host,InsecureSkipVerify: false},

func TestHtmlEmail(t *testing.T) {
	opt := &EmailOptions{
		User:     "",Password: "",To:       []string{""},From:     "",Addr:     "smtp.xxx.com:25",}

	auth := NewLoginAuth(opt.User,opt.Password)

	pic,err := ioutil.ReadFile(`C:Picturesexample.png`)
	if err != nil {
		t.Log(err)
	}
	dest := make([]byte,base64.StdEncoding.EncodedLen(len(pic)))
	base64.StdEncoding.Encode(dest,pic)

	buf := bytes.Buffer{}
	fmt.Fprint(&buf,"<html><head></head><body><p>Test png</p><p><img src="data:image/png;base64,")
	_,err = buf.Write(dest)
	fmt.Fprint(&buf,""></p><body></html>")

	err = sendMail(opt.Addr,auth,opt.From,opt.To,buf.Bytes(),"subject")
	if err != nil {
		t.Log(err)
	}

}

type LoginAuth struct {
	username,password string
}

func NewLoginAuth(username,password string) smtp.Auth {
	return &LoginAuth{username,password}
}

func (a *LoginAuth) Start(server *smtp.ServerInfo) (string,[]byte,error) {
	return "LOGIN",[]byte{},nil
}

func (a *LoginAuth) Next(fromServer []byte,more bool) ([]byte,error) {
	if more {
		switch string(fromServer) {
		case "Username:":
			return []byte(a.username),nil
		case "Password:":
			return []byte(a.password),nil
		default:
			return nil,errors.New("Unknown fromServer")
		}
	}
	return nil,nil
}

func sendMail(addr string,auth smtp.Auth,from string,to []string,msg []byte,subject string) error {
	conn,err := smtp.Dial(addr)
	host,_,_ := net.SplitHostPort(addr)
	if err != nil {
		return err
	}
	defer conn.Close()

	if ok,_ := conn.Extension("STARTTLS"); ok {
		config := &tls.Config{ServerName: host,InsecureSkipVerify: true}
		if err = conn.StartTLS(config); err != nil {
			return err
		}
	}

	if auth != nil {
		if ok,_ := conn.Extension("AUTH"); ok {
			if err = conn.Auth(auth); err != nil {
				return err
			}
		}
	}
	if err = conn.Mail(from); err != nil {
		return err
	}
	for _,addr := range to {
		if err = conn.Rcpt(addr); err != nil {
			return err
		}
	}
	w,err := conn.Data()
	if err != nil {
		return err
	}
	header := make(map[string]string)
	header["Subject"] = subject
	header["MIME-Version"] = "1.0"
	header["To"] = strings.Join(to,",")
	header["From"] = from
	header["Content-Type"] = "text/HTML; charset="utf-8""
	header["Content-Transfer-Encoding"] = "base64"
	message := ""
	for k,v := range header {
		message += fmt.Sprintf("%s: %srn",k,v)
	}
	message += "rn" + base64.StdEncoding.EncodeToString(msg)
	_,err = w.Write([]byte(message))
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return conn.Quit()
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读