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 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去
    • ¥20 用matlab的pdetool解决以下三个问题
    • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
    • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
    • ¥20 h3c静态路要求有详细过程