dqf67993 2016-06-28 02:12
浏览 419
已采纳

Golang使用gomail.v2的多个收件人

The only way that I've used to send emails via gomail.v2 is using the Send() function to each email in a for loop. But I need to show the other email addresses those the same email have been sent.

for _, recipient := range os.Args[3:] {
    mail.SetAddressHeader("From", "my@mail.com", "My Name")
    mail.SetHeader("To", recipient)
    mail.SetHeader("Subject", os.Args[2])
    mail.SetBody("text/html", os.Args[1])

    if err := dialer.DialAndSend(mail); err != nil {
        log.Printf("Could not send email to %q: %v", recipient, err)
        panic(err)
    }
}

I've found something like:

var emails bytes.Buffer

mail.SetAddressHeader("From", "my@mail.com.br", "My Name")
mail.SetHeader("Subject", os.Args[2])
mail.SetBody("text/html", os.Args[1])

for _, recipient := range os.Args[3:] {
    emails.WriteString(recipient + ",")
}

mail.SetHeader("To", emails.String())

if err := dialer.DialAndSend(mail); err != nil {
    log.Printf("Could not send email to %q: %v", buffer, err)
    panic(err)
}

It works by sending the email only to the first recipient in the String. And in the email manager like Gmail, Outlook or any other the other recipients addresses are showed, but not sent.

How should I do that properly?

  • 写回答

1条回答 默认 最新

  • drfcaw7460 2016-06-28 06:52
    关注

    Give this a try?

    recipients := os.Args[3:]
    addresses := make([]string, len(recipients))
    for i, recipient := range recipients {
        addresses[i] = mail.FormatAddress(recipient, "")
    }
    
    mail.SetHeader("To", addresses...)
    
    if err := dialer.DialAndSend(mail); err != nil {
        log.Fatal(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了