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 (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Send sends out the mail message
|
|
|
|
func (c *Client) Send(ml ...*Msg) error {
|
|
|
|
var errs []error
|
|
|
|
if err := c.checkConn(); err != nil {
|
|
|
|
return fmt.Errorf("failed to send mail: %w", err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
errs = append(errs, ErrServerNoUnencoded)
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrServerNoUnencoded}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := m.GetSender(false)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrGetSender, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rl, err := m.GetRecipients()
|
|
|
|
if err != nil {
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrGetRcpts, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.mail(f); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("sending MAIL FROM command failed: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrSMTPMailFrom, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
if reserr := c.sc.Reset(); reserr != nil {
|
|
|
|
errs = append(errs, reserr)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
failed := false
|
2022-12-31 12:40:42 +01:00
|
|
|
rse := SendError{}
|
|
|
|
rse.details = make([]error, 0)
|
|
|
|
rse.rcpt = make([]string, 0)
|
2022-12-10 13:41:00 +01:00
|
|
|
for _, r := range rl {
|
|
|
|
if err := c.rcpt(r); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("sending RCPT TO command failed: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
rse.Err = ErrSMTPRcptTo
|
|
|
|
rse.details = append(rse.details, err)
|
|
|
|
rse.rcpt = append(rse.rcpt, r)
|
2022-12-10 13:41:00 +01:00
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
if reserr := c.sc.Reset(); reserr != nil {
|
|
|
|
errs = append(errs, reserr)
|
|
|
|
}
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = rse
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, err := c.sc.Data()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("sending DATA command failed: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrSMTPData, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = m.WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("sending mail content failed: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrWriteContent, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to close DATA writer: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrSMTPDataClose, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Reset(); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("sending RSET command failed: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrSMTPReset, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.checkConn(); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to check server connection: %w", err))
|
2022-12-31 12:40:42 +01:00
|
|
|
m.sendError = SendError{Err: ErrConnCheck, details: []error{err}}
|
2022-12-10 13:41:00 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
errtxt := ""
|
|
|
|
for i := range errs {
|
|
|
|
errtxt += fmt.Sprintf("%s", errs[i])
|
|
|
|
if i < len(errs) {
|
|
|
|
errtxt += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s", errtxt)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|