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", "[email protected]", "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", "[email protected]", "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?
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With