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

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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料