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"
|
|
|
|
)
|
|
|
|
|
2024-10-05 11:02:26 +02:00
|
|
|
// Send attempts to send one or more Msg using the Client connection to the SMTP server.
|
2024-10-06 13:41:45 +02:00
|
|
|
// If the Client has no active connection to the server, Send will fail with an error. For each
|
|
|
|
// of the provided Msg, it will associate a SendError with the Msg in case of a transmission
|
|
|
|
// or delivery error.
|
|
|
|
//
|
|
|
|
// This method first checks for an active connection to the SMTP server. If the connection is
|
|
|
|
// not valid, it returns an error wrapped in a SendError. It then iterates over the provided
|
|
|
|
// messages, attempting to send each one. If an error occurs during sending, the method records
|
|
|
|
// the error and associates it with the corresponding Msg.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - messages: A variadic list of pointers to Msg objects to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error that aggregates any SendErrors encountered during the sending process; otherwise, returns nil.
|
2024-02-27 11:14:33 +01:00
|
|
|
func (c *Client) Send(messages ...*Msg) (returnErr error) {
|
2024-11-14 10:17:18 +01:00
|
|
|
escSupport := false
|
|
|
|
if c.smtpClient != nil {
|
|
|
|
escSupport, _ = c.smtpClient.Extension("ENHANCEDSTATUSCODES")
|
|
|
|
}
|
2022-12-10 13:41:00 +01:00
|
|
|
if err := c.checkConn(); err != nil {
|
2024-11-14 10:17:18 +01:00
|
|
|
returnErr = &SendError{Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
errcode: errorCode(err), enhancedStatusCode: enhancedStatusCode(err, escSupport)}
|
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
|
|
|
|
}
|