doujie4787
2017-09-22 03:30
浏览 53
已采纳

Gomail.v2-切片中的多个收件人

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: "fromuser@foo.com",
                FromFirstLast: "From User",
                ToMail: []string{"user1@foo.com", "user2@foo.com"},
                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
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpuwov1487 2017-09-22 04:27
    已采纳

    package gomail

    import "gopkg.in/gomail.v2"
    

    func (*Message) SetAddressHeader

    func (m *Message) SetAddressHeader(field, address, name string)
    

    SetAddressHeader sets an address to the given header field.

    func (*Message) SetHeader

    func (m *Message) SetHeader(field string, value ...string)
    

    SetHeader sets a value to the given header field.

    The example you referenced and the documentation has, for multiple addresses:

    addresses := make([]string, len(e.ToMail))
    for i := range addresses {
        addresses[i] = m.FormatAddress(e.ToMail[i], "")
    }
    m.SetHeader("To", addresses...)
    

    But you write, using the single address (with implicit FormatAddress) form:

    m.SetAddressHeader("To", addresses...)
    

    Error:

    not enough arguments in call to m.SetAddressHeader
            have (string, []string...)
            want (string, string, string)
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题