go html提取纯文本_Go语言中发送邮件

简介

在通知中,邮件算是最常见的通知方式了,在Go语言中,提供了net/smtp包可以直接发送邮件。

14335e4db2609c9203e8523f3d5c0b61.png

代码

默认是纯文本类型的,我们可以通过传contentTyoe这个头信息来告诉收件者,内容是什么格式的。

package email​import (    "net/smtp"    "strings")​// SendMail 发送邮件func SendMail(name, user, password, host, port, toEmails, subject, body, mailtype string) error {    auth := smtp.PlainAuth("", user, password, host)    var contentType string    if mailtype == "html" {        contentType = "Content-Type: text/" + mailtype + "; charset=UTF-8"    } else {        contentType = "Content-Type: text/plain" + "; charset=UTF-8"    }    msg := []byte("to: " + toEmails + "from: " + name + " subject: " + subject + "" + contentType + "" + body)    sendTo := strings.Split(toEmails, ";")    err := smtp.SendMail(host+":"+port, auth, user, sendTo, msg)    return err}
f419be2ed94fdeca75e3b09597508b9c.png

测试

package main​import (    "fmt"    "email")​func main() {    content := `        

linux OPcai

` err := email.SendMail("linux运维菜", "opcai@linux.com", "opcai@2020", "smtp.linux.com", "25", "caicai@linux.com", "这个一个测试邮件", content, "html") if err != nil { fmt.Println("发送失败") panic(err) } fmt.Println("发送成功")}

总结

如果需要更丰富的内容,可以找第三方开源的包,自带的包都是要自己处理的。

2936273389321ed37917aefb6c7486c4.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值