为了账号安全,请及时绑定邮箱和手机立即绑定

Go Golang SMTP 脚本。需要添加密件抄送头

Go Golang SMTP 脚本。需要添加密件抄送头

Go
aluckdog 2023-06-01 17:00:34
我在 Go 中为 SMTP 使用这个众所周知的空白画布。我需要在 Bcc Blank Carbon Copy 地址中添加它,但我已经尝试了很多东西,但我尝试的任何东西都不起作用,这很奇怪......我尝试添加 "headers["Bcc"] = "someemail@address.com" 我相信这是一个简单的修改。提前致谢..package mainimport (   "fmt"   "log"   "net"   "net/mail"   "net/smtp"   "crypto/tls")func main() {from := mail.Address{"", "username@example.tld"}to   := mail.Address{"", "username@anotherexample.tld"}subj := "This is the email subject"body := "This is an example body.\n With two lines."headers := make(map[string]string)headers["From"] = from.String()headers["To"] = to.String()headers["Subject"] = subjmessage := ""for k,v := range headers {    message += fmt.Sprintf("%s: %s\r\n", k, v)}message += "\r\n" + bodyservername := "smtp.example.tld:465"host, _, _ := net.SplitHostPort(servername)auth := smtp.PlainAuth("","username@example.tld", "password", host)tlsconfig := &tls.Config {    InsecureSkipVerify: true,    ServerName: host,}conn, err := tls.Dial("tcp", servername, tlsconfig)if err != nil {    log.Panic(err)}c, err := smtp.NewClient(conn, host)if err != nil {    log.Panic(err)}if err = c.Auth(auth); err != nil {    log.Panic(err)}if err = c.Mail(from.Address); err != nil {    log.Panic(err)}if err = c.Rcpt(to.Address); err != nil {    log.Panic(err)}w, err := c.Data()if err != nil {    log.Panic(err)}_, err = w.Write([]byte(message))if err != nil {    log.Panic(err)}err = w.Close()if err != nil {    log.Panic(err)}c.Quit()
查看完整描述

3 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

请参阅smtp#SendMail 包文档中的以下部分

msg 参数应该是一个 RFC 822 风格的电子邮件,首先是标题,一个空行,然后是邮件正文。msg 行应该以 CRLF 终止。消息标头通常应包括“发件人”、“收件人”、“主题”和“抄送”等字段。发送“密件抄送”消息是通过在 to 参数中包含电子邮件地址但不将其包含在消息标头中来完成的。

换句话说,不要将它们添加到标题中,而只是添加到收件人列表中。

在您的示例样板代码中,您将为密件抄送列表中的每封电子邮件添加一个调用c.Rcpt(...),仅此而已。没有什么可以添加到标题中。


查看完整回答
反对 回复 2023-06-01
?
当年话下

TA贡献1890条经验 获得超9个赞

您还需要RCPT为 Bcc 地址添加一行,例如:

if err = c.Rcpt("someemail@address.com"); err != nil {
    log.Panic(err)
}


查看完整回答
反对 回复 2023-06-01
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

该标准不包括抄送的概念。您需要为每个收件人添加一个 RCPT TO,然后将 bcc: sample@email.com 添加到正文。客户将负责其余的工作。



查看完整回答
反对 回复 2023-06-01
  • 3 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信