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
|
|
|
|
|
2024-11-22 15:38:04 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/wneessen/go-mail/smtp"
|
|
|
|
)
|
2024-09-20 16:40:12 +02:00
|
|
|
|
2024-11-22 15:55:45 +01:00
|
|
|
// SendWithSMTPClient attempts to send one or more Msg using a provided smtp.Client with an
|
|
|
|
// established connection to the SMTP server. If the smtp.Client has no active connection to
|
|
|
|
// the server, SendWithSMTPClient 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.
|
2024-10-06 13:41:45 +02:00
|
|
|
//
|
|
|
|
// This method first checks for an active connection to the SMTP server. If the connection is
|
|
|
|
// not valid, it returns 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. If multiple errors are encountered, it aggregates
|
|
|
|
// them into a single SendError to be returned.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2024-11-22 15:55:45 +01:00
|
|
|
// - client: A pointer to the smtp.Client that holds the connection to the SMTP server
|
2024-10-06 13:41:45 +02:00
|
|
|
// - messages: A variadic list of pointers to Msg objects to be sent.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - An error that represents the sending result, which may include multiple SendErrors if
|
|
|
|
// any occurred; otherwise, returns nil.
|
2024-11-22 15:38:04 +01:00
|
|
|
func (c *Client) SendWithSMTPClient(client *smtp.Client, messages ...*Msg) error {
|
2024-11-13 23:02:30 +01:00
|
|
|
escSupport := false
|
2024-11-22 15:38:04 +01:00
|
|
|
if client != nil {
|
|
|
|
escSupport, _ = client.Extension("ENHANCEDSTATUSCODES")
|
2024-11-13 23:02:30 +01:00
|
|
|
}
|
2024-11-22 15:38:04 +01:00
|
|
|
if err := c.checkConn(client); err != nil {
|
2024-11-14 10:20:52 +01:00
|
|
|
return &SendError{
|
|
|
|
Reason: ErrConnCheck, errlist: []error{err}, isTemp: isTempError(err),
|
|
|
|
errcode: errorCode(err), enhancedStatusCode: enhancedStatusCode(err, escSupport),
|
|
|
|
}
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
2023-01-02 12:14:14 +01:00
|
|
|
var errs []*SendError
|
2024-09-20 16:40:12 +02:00
|
|
|
for id, message := range messages {
|
2024-11-22 15:38:04 +01:00
|
|
|
if sendErr := c.sendSingleMsg(client, message); sendErr != nil {
|
2024-09-19 10:56:01 +02:00
|
|
|
messages[id].sendError = sendErr
|
2024-09-20 16:40:12 +02:00
|
|
|
|
|
|
|
var msgSendErr *SendError
|
|
|
|
if errors.As(sendErr, &msgSendErr) {
|
|
|
|
errs = append(errs, msgSendErr)
|
|
|
|
}
|
2022-12-10 13:41:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-11-13 23:04:39 +01:00
|
|
|
// We assume that the error codes and flags 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
|
2024-11-13 23:04:39 +01:00
|
|
|
returnErr.errcode = errs[len(errs)-1].errcode
|
|
|
|
returnErr.enhancedStatusCode = errs[len(errs)-1].enhancedStatusCode
|
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
|
|
|
|
}
|