SendGrid SMTP邮件在Go中无法将邮件发送到抄送(CC)和密送(BCC)地址。

huangapple go评论97阅读模式
英文:

SendGrid SMTP emails are not delivered to CC and BCC addresses in Go

问题

我正在尝试发送带有抄送(CC)和密送(BCC)的电子邮件。但是CC和BCC地址的邮件未能送达。但是CC地址会显示给收件人。下面是一个快照。

我正在使用golang的net/smtpSendGrid来发送邮件。下面是我的代码示例:

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()
}

请帮助我。提前谢谢 SendGrid SMTP邮件在Go中无法将邮件发送到抄送(CC)和密送(BCC)地址。

英文:

I am trying to send an email with CC & BCC. Emails are not delivered to CC & BCC addresses. But CC addresses are displayed to the recipient's details. A snapshot is given below.

SendGrid SMTP邮件在Go中无法将邮件发送到抄送(CC)和密送(BCC)地址。

I am using golang net/smtp with SendGrid. My code sample is given below.

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()
}

Please help me. Advance thanks SendGrid SMTP邮件在Go中无法将邮件发送到抄送(CC)和密送(BCC)地址。

答案1

得分: 2

在底层,smtp.SendMail为每个to收件人调用smtp.Client.Rcpt
to切片指定了实际接收电子邮件的人。电子邮件正文中的收件人仅用于信息提供 - 实际上,它们甚至不需要与真实的收件人信息匹配。

因此,要解决您的寻址问题,您需要收集所有的toccbcc收件人:

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, ",")))
//}
英文:

Under the covers, smtp.SendMail calls smtp.Client.Rcpt for each to recipient.
The to slice directs who will actually receive the email. The addressees in the body of the email is purely informational - in fact they don't even need to match the real addressee info.

So to fix your addressing issue, you need to collect all to, cc & bcc addressees:

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,
)

also since every recipient receives the body of the e-mail, bcc users should not be listed in the
body, for obvious privacy reasons.

So remove this:

// if len(m.BCC) > 0 {
//    buf.WriteString(fmt.Sprintf("Bcc: %s\r\n", strings.Join(m.BCC, ",")))
//}

huangapple
  • 本文由 发表于 2021年10月30日 20:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/69779712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定