gomail【draft】

2020/02/08 Go 知识点

发送附件及内容

        attach := "job/log/test.csv"
		f, err := os.Create(attach) // 创建文件,已存在将会清空文件
		if err != nil {
			panic(err)
		}
		defer f.Close()

		f.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM

		w := csv.NewWriter(f)
		data := make([][]string, 1)
		data[0] = []string{"流量位","1","2","3"}
		w.WriteAll(data)
		w.Flush()

		content := "流量位<br /><br />"
		showList := whiteList
		content = []string{1,2,3}
		QuickSendMail( content, attach)
		
				
// 邮件发送
func QuickSendMail( body string, attach string) {
	var mailTo []string
    var subject string
    mailTo = []string{"12222@qq.com"}
    subject = "测试邮件"
   

	// 服务邮箱配置
	mailConn := map[string]string{
		"user": "xxx",
		"pass": "xxx",
		"host": "XXX",
		"port": "xxx",
	}
	port, _ := strconv.Atoi(mailConn["port"])
	mail := gomail.NewMessage()
	mail.SetHeader("From", "walkingsun"+"<"+mailConn["user"]+">")
	mail.SetHeader("To", mailTo...)
	mail.SetHeader("Subject", subject)
	mail.SetBody("text/html", body)
	if len(attach) > 0 {
		mail.Attach(attach)
	}

	dialer := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
	err := dialer.DialAndSend(mail)
	if err != nil {
		fmt.Println(fmt.Sprintf("邮件发送出现错误, 错误原因为:%s, 邮件标题为:%s, 邮件内容为:%s", err, subject, body))
	}
	return
}		

Search

    Table of Contents