2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-12-10 13:41:00 +01:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
//go:build !go1.20
|
|
|
|
// +build !go1.20
|
|
|
|
|
|
|
|
package mail
|
|
|
|
|
2023-01-18 10:30:06 +01:00
|
|
|
import "strings"
|
|
|
|
|
2022-12-10 13:41:00 +01:00
|
|
|
// Send sends out the mail message
|
|
|
|
func (c *Client) Send(ml ...*Msg) error {
|
2023-01-02 12:14:14 +01:00
|
|
|
if cerr := c.checkConn(); cerr != nil {
|
|
|
|
return &SendError{Reason: ErrConnCheck, errlist: []error{cerr}, isTemp: isTempError(cerr)}
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2023-01-02 12:14:14 +01:00
|
|
|
var errs []*SendError
|
2022-12-10 13:41:00 +01:00
|
|
|
for _, m := range ml {
|
2022-12-31 12:46:11 +01:00
|
|
|
m.sendError = nil
|
2022-12-10 13:41:00 +01:00
|
|
|
if m.encoding == NoEncoding {
|
|
|
|
if ok, _ := c.sc.Extension("8BITMIME"); !ok {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrNoUnencoded, isTemp: false}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := m.GetSender(false)
|
|
|
|
if err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrGetSender, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rl, err := m.GetRecipients()
|
|
|
|
if err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrGetRcpts, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:30:06 +01:00
|
|
|
if c.dsn {
|
|
|
|
if c.dsnmrtype != "" {
|
|
|
|
c.sc.SetDSNMailReturnOption(string(c.dsnmrtype))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.sc.Mail(f); err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
|
2022-12-10 13:41:00 +01:00
|
|
|
if reserr := c.sc.Reset(); reserr != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se.errlist = append(se.errlist, reserr)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2023-01-02 12:14:14 +01:00
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
failed := false
|
2023-01-01 14:20:13 +01:00
|
|
|
rse := &SendError{}
|
|
|
|
rse.errlist = make([]error, 0)
|
2022-12-31 12:40:42 +01:00
|
|
|
rse.rcpt = make([]string, 0)
|
2023-01-18 10:30:06 +01:00
|
|
|
rno := strings.Join(c.dsnrntype, ",")
|
|
|
|
c.sc.SetDSNRcptNotifyOption(rno)
|
2022-12-10 13:41:00 +01:00
|
|
|
for _, r := range rl {
|
2023-01-18 10:30:06 +01:00
|
|
|
if err := c.sc.Rcpt(r); err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
rse.Reason = ErrSMTPRcptTo
|
|
|
|
rse.errlist = append(rse.errlist, err)
|
2022-12-31 12:40:42 +01:00
|
|
|
rse.rcpt = append(rse.rcpt, r)
|
2023-01-01 14:20:13 +01:00
|
|
|
rse.isTemp = isTempError(err)
|
2022-12-10 13:41:00 +01:00
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
if reserr := c.sc.Reset(); reserr != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
rse.errlist = append(rse.errlist, err)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = rse
|
2023-01-02 12:14:14 +01:00
|
|
|
errs = append(errs, rse)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, err := c.sc.Data()
|
|
|
|
if err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = m.WriteTo(w)
|
|
|
|
if err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrWriteContent, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Reset(); err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.checkConn(); err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
se := &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
m.sendError = se
|
|
|
|
errs = append(errs, se)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2023-01-02 12:14:14 +01:00
|
|
|
if len(errs) > 1 {
|
|
|
|
re := &SendError{Reason: ErrAmbiguous}
|
|
|
|
for i := range errs {
|
2023-01-02 20:49:35 +01:00
|
|
|
re.errlist = append(re.errlist, errs[i].errlist...)
|
|
|
|
re.rcpt = append(re.rcpt, errs[i].rcpt...)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2023-01-02 12:14:14 +01:00
|
|
|
|
2023-01-02 22:21:23 +01:00
|
|
|
// We assume that the isTemp flag from the last error we received should be the
|
2023-01-02 12:14:14 +01:00
|
|
|
// indicator for the returned isTemp flag as well
|
|
|
|
re.isTemp = errs[len(errs)-1].isTemp
|
|
|
|
|
|
|
|
return re
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2023-01-02 12:14:14 +01:00
|
|
|
return errs[0]
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|