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
|
2024-02-27 11:10:49 +01:00
|
|
|
func (c *Client) Send(messages ...*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
|
2024-02-27 11:10:49 +01:00
|
|
|
for _, message := range messages {
|
|
|
|
message.sendError = nil
|
|
|
|
if message.encoding == NoEncoding {
|
2024-02-24 22:06:18 +01:00
|
|
|
if ok, _ := c.smtpClient.Extension("8BITMIME"); !ok {
|
2024-02-27 11:10:49 +01:00
|
|
|
sendErr := &SendError{Reason: ErrNoUnencoded, isTemp: false}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
from, err := message.GetSender(false)
|
2022-12-10 13:41:00 +01:00
|
|
|
if err != nil {
|
2024-02-27 11:10:49 +01:00
|
|
|
sendErr := &SendError{Reason: ErrGetSender, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
rcpts, err := message.GetRecipients()
|
2022-12-10 13:41:00 +01:00
|
|
|
if err != nil {
|
2024-02-27 11:10:49 +01:00
|
|
|
sendErr := &SendError{Reason: ErrGetRcpts, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-18 10:30:06 +01:00
|
|
|
if c.dsn {
|
|
|
|
if c.dsnmrtype != "" {
|
2024-02-24 22:06:18 +01:00
|
|
|
c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
|
2023-01-18 10:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
if err = c.smtpClient.Mail(from); err != nil {
|
|
|
|
sendErr := &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
sendErr.errlist = append(sendErr.errlist, resetSendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
failed := false
|
2024-02-27 11:10:49 +01:00
|
|
|
rcptSendErr := &SendError{}
|
|
|
|
rcptSendErr.errlist = make([]error, 0)
|
|
|
|
rcptSendErr.rcpt = make([]string, 0)
|
|
|
|
rcptNotifyOpt := strings.Join(c.dsnrntype, ",")
|
|
|
|
c.smtpClient.SetDSNRcptNotifyOption(rcptNotifyOpt)
|
|
|
|
for _, rcpt := range rcpts {
|
|
|
|
if err = c.smtpClient.Rcpt(rcpt); err != nil {
|
|
|
|
rcptSendErr.Reason = ErrSMTPRcptTo
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, err)
|
|
|
|
rcptSendErr.rcpt = append(rcptSendErr.rcpt, rcpt)
|
|
|
|
rcptSendErr.isTemp = isTempError(err)
|
2022-12-10 13:41:00 +01:00
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
2024-02-27 11:10:49 +01:00
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, err)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
message.sendError = rcptSendErr
|
|
|
|
errs = append(errs, rcptSendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
writer, err := c.smtpClient.Data()
|
2022-12-10 13:41:00 +01:00
|
|
|
if err != nil {
|
2024-02-27 11:10:49 +01:00
|
|
|
sendErr := &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
_, err = message.WriteTo(writer)
|
2022-12-10 13:41:00 +01:00
|
|
|
if err != nil {
|
2024-02-27 11:10:49 +01:00
|
|
|
sendErr := &SendError{Reason: ErrWriteContent, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
message.isDelivered = true
|
2022-12-10 13:41:00 +01:00
|
|
|
|
2024-02-27 11:10:49 +01:00
|
|
|
if err = writer.Close(); err != nil {
|
|
|
|
sendErr := &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:10:49 +01:00
|
|
|
if err = c.Reset(); err != nil {
|
|
|
|
sendErr := &SendError{Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-02-27 11:10:49 +01:00
|
|
|
if err = c.checkConn(); err != nil {
|
|
|
|
sendErr := &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
message.sendError = sendErr
|
|
|
|
errs = append(errs, sendErr)
|
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 {
|
2024-02-27 11:10:49 +01:00
|
|
|
returnErr := &SendError{Reason: ErrAmbiguous}
|
2023-01-02 12:14:14 +01:00
|
|
|
for i := range errs {
|
2024-02-27 11:10:49 +01:00
|
|
|
returnErr.errlist = append(returnErr.errlist, errs[i].errlist...)
|
|
|
|
returnErr.rcpt = append(returnErr.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
|
2024-02-27 11:10:49 +01:00
|
|
|
returnErr.isTemp = errs[len(errs)-1].isTemp
|
2023-01-02 12:14:14 +01:00
|
|
|
|
2024-02-27 11:10:49 +01:00
|
|
|
return returnErr
|
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
|
|
|
|
}
|