2022-12-10 13:41:00 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Winni Neessen <winni@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
//go:build go1.20
|
|
|
|
// +build go1.20
|
|
|
|
|
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Send sends out the mail message
|
|
|
|
func (c *Client) Send(ml ...*Msg) (rerr error) {
|
|
|
|
if err := c.checkConn(); err != nil {
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
2022-12-10 13:41:00 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrNoUnencoded, isTemp: false}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := m.GetSender(false)
|
|
|
|
if err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrGetSender, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rl, err := m.GetRecipients()
|
|
|
|
if err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrGetRcpts, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.mail(f); err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
if reserr := c.sc.Reset(); reserr != nil {
|
|
|
|
rerr = errors.Join(rerr, reserr)
|
|
|
|
}
|
|
|
|
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)
|
2022-12-10 13:41:00 +01:00
|
|
|
for _, r := range rl {
|
|
|
|
if err := c.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 {
|
|
|
|
rerr = errors.Join(rerr, reserr)
|
|
|
|
}
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = rse
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, err := c.sc.Data()
|
|
|
|
if err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = m.WriteTo(w)
|
|
|
|
if err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrWriteContent, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Reset(); err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.checkConn(); err != nil {
|
2023-01-01 14:20:13 +01:00
|
|
|
m.sendError = &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
2023-01-02 12:14:14 +01:00
|
|
|
rerr = errors.Join(rerr, m.sendError)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|