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

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集