go-gomail / gomail

The best way to send emails in Go.
MIT License
4.32k stars 572 forks source link

Multiple mails in CC header #93

Open anasanzari opened 7 years ago

anasanzari commented 7 years ago

Can we have a method for adding multiple mails in cc header?

snailQH commented 7 years ago

I read the source code and find it supports the multi-mails in cc list. But in a indirect way. I paste some demo code here:


func dosendmail() {
    var tt []*gomail.Message //m ...*Message
    ccAdd := []string{0: "first@gmail.com", 1: "second@gmail.com"}
    for _, ccaddress := range ccAdd {
        m := gomail.NewMessage()
        m.SetHeader("From", fromAdd)
        m.SetHeader("To", toAdd)
        m.SetAddressHeader("Cc", ccaddress, "ccname")
        m.SetHeader("Subject", "Hello!")
        m.SetBody("text/html", "Hello <b>first</b> and <i>second</i>!")
        m.Attach("test.gofile") //attachment
        tt = append(tt, m)      // every mail added here
    }

    d := gomail.NewDialer(smtpAdd, 25, fromAdd, pwd)
    d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
    // Send the email to receiver, first and second.
    if err := d.DialAndSend(tt...); err != nil { //dial and sent via tt
        text := fmt.Sprintf("%v", err)
        if strings.Contains(text, "535 Error: authentication failed") { //sometimes it will fail in authentication,just retry
            fmt.Println(text, "\t", "retrying sending e-mail")
            dosendmail()
        }
    } else {
        fmt.Println("Success")
    }
}

Each one in the cc list will receive one mail which sent to the RECEIVER and cc to himself(can not see others who also in the cc list). AND, MORE IMPORTANT. THE RECEIVER MAY RECEIVE THE EMAIL MORE THAN ONE TIME DEPENDS ON THE NUMBERS IN THE CC LIST. You can add more than one email in the To like m.SetHeader("To", "bob@example.com", "cora@example.com") Or another way: m.SetHeader("Cc","bob@example.com","cora@example.com"), it can be parsed into cc list.

iamdanielyin commented 6 years ago

This works for me m.SetHeader("Cc", "bob@example.com", "cora@example.com")

crazy-canux commented 5 years ago

@yinfxs

what is the cc or to list can not hard code?

zheeeng commented 5 years ago

Have the same question

pedromorgan commented 5 years ago

see #104 and #108

awsp commented 5 years ago

@crazy-canux

Since SetHeader takes varargs, for non-hard coding way, should it be this way?

list := []string{"a@a.com", "b@b.com"}
m.SetHeader("Cc", list...)