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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-01-18 10:30:06 +01:00
|
|
|
"strings"
|
2022-12-10 13:41:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Send sends out the mail message
|
2024-02-27 11:14:33 +01:00
|
|
|
func (c *Client) Send(messages ...*Msg) (returnErr error) {
|
2022-12-10 13:41:00 +01:00
|
|
|
if err := c.checkConn(); err != nil {
|
2024-02-27 11:14:33 +01:00
|
|
|
returnErr = &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
2022-12-10 13:41:00 +01:00
|
|
|
return
|
|
|
|
}
|
2024-07-16 13:07:20 +02:00
|
|
|
|
|
|
|
var errs []error
|
|
|
|
defer func() {
|
|
|
|
returnErr = errors.Join(errs...)
|
|
|
|
}()
|
|
|
|
|
2024-09-18 12:29:42 +02:00
|
|
|
for id, message := range messages {
|
2024-09-18 11:06:48 +02:00
|
|
|
if sendErr := c.sendSingleMsg(message); sendErr != nil {
|
2024-09-18 12:29:42 +02:00
|
|
|
messages[id].sendError = sendErr
|
2024-09-18 11:06:48 +02:00
|
|
|
errs = append(errs, sendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
}
|
2022-12-10 13:41:00 +01:00
|
|
|
|
2024-09-18 11:06:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendSingleMsg sends out a single message and returns an error if the transmission/delivery fails.
|
|
|
|
// It is invoked by the public Send methods
|
|
|
|
func (c *Client) sendSingleMsg(message *Msg) error {
|
|
|
|
if message.encoding == NoEncoding {
|
|
|
|
if ok, _ := c.smtpClient.Extension("8BITMIME"); !ok {
|
|
|
|
return &SendError{Reason: ErrNoUnencoded, isTemp: false}
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
}
|
|
|
|
from, err := message.GetSender(false)
|
|
|
|
if err != nil {
|
|
|
|
return &SendError{Reason: ErrGetSender, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message}
|
|
|
|
}
|
|
|
|
rcpts, err := message.GetRecipients()
|
|
|
|
if err != nil {
|
|
|
|
return &SendError{Reason: ErrGetRcpts, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
affectedMsg: message}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.dsn {
|
|
|
|
if c.dsnmrtype != "" {
|
|
|
|
c.smtpClient.SetDSNMailReturnOption(string(c.dsnmrtype))
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
}
|
|
|
|
if err = c.smtpClient.Mail(from); err != nil {
|
|
|
|
retError := &SendError{Reason: ErrSMTPMailFrom, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
retError.errlist = append(retError.errlist, resetSendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
return retError
|
|
|
|
}
|
|
|
|
failed := false
|
|
|
|
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)
|
|
|
|
failed = true
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
if resetSendErr := c.smtpClient.Reset(); resetSendErr != nil {
|
|
|
|
rcptSendErr.errlist = append(rcptSendErr.errlist, resetSendErr)
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2024-09-18 11:06:48 +02:00
|
|
|
return rcptSendErr
|
|
|
|
}
|
|
|
|
writer, err := c.smtpClient.Data()
|
|
|
|
if err != nil {
|
|
|
|
return &SendError{Reason: ErrSMTPData, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
}
|
|
|
|
_, err = message.WriteTo(writer)
|
|
|
|
if err != nil {
|
|
|
|
return &SendError{Reason: ErrWriteContent, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
}
|
|
|
|
message.isDelivered = true
|
2022-12-10 13:41:00 +01:00
|
|
|
|
2024-09-18 11:06:48 +02:00
|
|
|
if err = writer.Close(); err != nil {
|
|
|
|
return &SendError{Reason: ErrSMTPDataClose, errlist: []error{err}, isTemp: isTempError(err)}
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
|
|
|
|
2024-09-18 11:06:48 +02:00
|
|
|
if err = c.Reset(); err != nil {
|
|
|
|
return &SendError{Reason: ErrSMTPReset, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
}
|
|
|
|
if err = c.checkConn(); err != nil {
|
|
|
|
return &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err)}
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|