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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算