Disclaimer: I have seen a similar question in this thread:
Golang multiple recipients with gomail.v2 but cannot get this to work, and do not yet have the juice to comment there asking for help. The op in that question is using os.Args[], whereas I want to use a slice stored in a config file.
I'm attempting to use the awesome gomail.v2 package to send to a list of multiple recipients contained in a slice ToMail in instance e of struct Email via my method Mail.
The compiler complains at line 37:
./mailer.go:37: not enough arguments in call to m.SetAddressHeader
have (string, []string...)
want (string, string, string)
How can I properly pass each recipient in my slice to the SetAddressHeader("To"... method so as to send to all recipients in slice ToMail?
What I've tried:
package main
import (
"fmt"
"gopkg.in/gomail.v2"
)
type Email struct {
FromMail, FromFirstLast, Password, Subject, Body, Server string
ToMail []string
Port int
}
func main() {
e := Email{FromMail: "[email protected]",
FromFirstLast: "From User",
ToMail: []string{"[email protected]", "[email protected]"},
Password: "passpasspass",
Subject: "Test",
Body:"TEST BODY",
Server: "foo.foomail.com",
Port: 587,}
err := e.Mail()
if err != nil {
fmt.Println(err)
}
}
func (e Email)Mail() (error) {
m := gomail.NewMessage()
m.SetAddressHeader("From", e.FromMail, e.FromFirstLast)
addresses := make([]string, len(e.ToMail))
for i, _ := range addresses{
addresses[i] = m.FormatAddress(e.ToMail[i], "")
}
m.SetAddressHeader("To", addresses...)
m.SetHeader("Subject", e.Subject)
m.SetBody("text/plain", e.Body)
d := gomail.NewPlainDialer(e.Server, e.Port, e.FromMail, e.Password)
if err := d.DialAndSend(m); err != nil {
return err
}
return nil
}
The example you referenced and the documentation has, for multiple addresses:
But you write, using the single address (with implicit
FormatAddress) form:Error: