我正在尝试使用 CC 和 BCC 发送电子邮件。电子邮件不会发送到抄送和密送地址。但抄送地址会显示到收件人的详细信息。下面给出一个快照。我将 golang net/smtp与SendGrid一起使用。我的代码示例如下。func SendEmail() error { email := entity.Email{ From: "info@somewhere.cloud", To: []string{"crajuceveifre-5717@yopmail.com"}, ReplyTo: "dileulobugre-7335@yopmail.com", Subject: "Email Subject", Body: "Email Body", BCC: []string{"lecrecezufeu-9078@yopmail.com"}, CC: []string{"houcroissezitri-3721@yopmail.com"}, } emailMessageBytes := ToBytes(email) smtpAuth := smtp.PlainAuth("", "apikey", config.Config.EmailClientApiKey, config.Config.EmailClientHost) err := smtp.SendMail(config.Config.EmailClientHost+":"+config.Config.EmailClientPort, smtpAuth, email.From, email.To, emailMessageBytes) if err != nil { log.Printf("smtp error: %s", err) return err } return nil}func ToBytes(m entity.Email) []byte { buf := bytes.NewBuffer(nil) buf.WriteString(fmt.Sprintf("From: %s\r\n", m.From)) buf.WriteString(fmt.Sprintf("To: %s\r\n", strings.Join(m.To, ","))) buf.WriteString(fmt.Sprintf("Reply-To: %s\r\n", m.ReplyTo)) if len(m.CC) > 0 { buf.WriteString(fmt.Sprintf("Cc: %s\r\n", strings.Join(m.CC, ","))) } if len(m.BCC) > 0 { buf.WriteString(fmt.Sprintf("Bcc: %s\r\n", strings.Join(m.BCC, ","))) } buf.WriteString(fmt.Sprintf("Subject: %s\r\n", m.Subject)) buf.WriteString("MIME-Version: 1.0\n") writer := multipart.NewWriter(buf) boundary := writer.Boundary() buf.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\n\n", boundary)) buf.WriteString(fmt.Sprintf("--%s\n", boundary)) buf.WriteString("Content-Type: text/html; charset=utf-8\n") buf.WriteString(m.Body) buf.WriteString(fmt.Sprintf("\n\n--%s\n", boundary)) return buf.Bytes()}请帮我。提前谢谢:)
1 回答
收到一只叮咚
TA贡献1821条经验 获得超4个赞
在幕后,smtp.SendMail调用smtp.Client.Rcpt每个to收件人。to切片指示谁将实际收到电子邮件。电子邮件正文中的收件人纯粹是信息性的——事实上,他们甚至不需要匹配真实的收件人信息。
因此,要解决您的寻址问题,您需要收集所有to, cc&bcc收件人:
var all []string
for _, a := range [][]string{email.To, email.CC, email.BCC} {
all = append(all, a)
}
err := smtp.SendMail(
config.Config.EmailClientHost+":"+config.Config.EmailClientPort,
smtpAuth,
email.From,
all, //email.To,
emailMessageBytes,
)
此外,由于每个收件人都会收到电子邮件的正文,出于明显的隐私原因,bcc用户不应列在正文中。
所以删除这个:
// if len(m.BCC) > 0 {
// buf.WriteString(fmt.Sprintf("Bcc: %s\r\n", strings.Join(m.BCC, ",")))
//}
- 1 回答
- 0 关注
- 167 浏览
添加回答
举报
0/150
提交
取消