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

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?

图片转代码服务由CSDN问答提供 功能建议

我过去一直通过gomail.v2发送电子邮件的唯一方法是对每个对象使用Send()函数 在for循环中发送电子邮件。 但是我需要显示发送相同电子邮件的其他电子邮件地址。

 对于_,收件人:=范围os.Args [3:] {
邮件。  SetAddressHeader(“发件人”,“ my@mail.com”,“我的名字”)
 mail.SetHeader(“收件人”,收件人)
 mail.SetHeader(“主题”,os.Args [2])
  mail.SetBody(“ text / html”,os.Args [1])
 
如果err:= dialer.DialAndSend(mail);  err!= nil {
 log.Printf(“无法将电子邮件发送到%q:%v”,收件人,err)
恐慌(err)
} 
} 
    
 
 

我发现了类似的内容:

  var emails bytes.Buffer 
 
mail.SetAddressHeader(“ From”,“ my@mail.com  .br“,“我的名字”)
mail.SetHeader(” Subject“,os.Args [2])
mail.SetBody(” text / html“,os.Args [1])
 
for _,收件人 := range os.Args [3:] {
 emails.WriteString(recipient +“,”)
} 
 
mail.SetHeader(“ To”,emails.String())
 
if err:=  dialer.DialAndSend(邮件);  err!= nil {
 log.Printf(“无法将电子邮件发送到%q:%v”,缓冲区,err)
恐慌(err)
} 
   
 \  n 

它的工作方式是仅将电子邮件发送给字符串中的第一个收件人。 并且在电子邮件管理器(如Gmail,Outlook或其他任何其他收件人)中,显示但未发送。 \ n

我应该如何正确做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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)
    }
    
    点赞 评论

相关推荐 更多相似问题