go-gomail / gomail

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

Multiple attachments will be overwritten! #126

Open anbaoyong opened 5 years ago

anbaoyong commented 5 years ago

When I send two attachments at the same time, the content of the A attachment will be overwritten by the content of the attachment B, and the content of the attachment B is empty My code is as follows,i appreciate it.

func SendMail(subject, body string, to []string, file map[string]*os.File) {

user := "test@test.com"
password := "123456"
m := gomail.NewMessage()
m.SetHeaders(map[string][]string{
    "From":    {m.FormatAddress("test@test.com", "test")},
    "To":      to,
    "Subject": {subject},
})
m.SetBody("text/html", body)
for fname, f := range file {
    m.Attach(fname, gomail.SetCopyFunc(func(w io.Writer) error {
        _, err := io.Copy(w, f)
        defer f.Close()
        return err
    }))
}

d := gomail.NewDialer("test@test.com", 587, user, password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
if err := d.DialAndSend(m); err != nil {
    log.Println(err)
}

}

func main() {

filemap := map[string]*os.File{}
f1,_:=os.Open("/control.sh")
f2,_:=os.Open("/build.sh")
filemap["control.sh"]=f1
filemap["build.sh"]=f2
SendMail("subject", "body", []string{"test@test.com"}, filemap)

}

pedromorgan commented 5 years ago

see #104 and #108