Improve error handling in autoForwardMessage and ensure proper client closure in createForwardingSMTPClient

This commit is contained in:
Ralph Slooten
2026-05-09 17:16:52 +12:00
parent da8eb3ece8
commit 6e2c42d2bc
2 changed files with 9 additions and 4 deletions

View File

@@ -20,7 +20,7 @@ func autoForwardMessage(from string, data *[]byte) error {
}
if err := forward(from, *data); err != nil {
return errors.WithMessage(err, "[forward] error: %s")
return fmt.Errorf("[forward] error: %w", err)
}
logger.Log().Debugf(
@@ -59,6 +59,7 @@ func createForwardingSMTPClient(config config.SMTPForwardConfigStruct, addr stri
// Set the hostname for HELO/EHLO
if hostname, err := os.Hostname(); err == nil {
if err := client.Hello(hostname); err != nil {
_ = client.Close()
return nil, fmt.Errorf("error saying HELO/EHLO to %s: %v", addr, err)
}
}

View File

@@ -882,10 +882,14 @@ func (s *session) readData() ([]byte, error) {
// TODO: Work out what to do with multiple to addresses.
func (s *session) makeHeaders(to []string) []byte {
var buffer bytes.Buffer
if len(to) == 0 {
return buffer.Bytes()
}
now := time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700 (MST)")
buffer.WriteString(fmt.Sprintf("Received: from %s (%s [%s])\r\n", s.remoteName, s.remoteHost, s.remoteIP))
buffer.WriteString(fmt.Sprintf(" by %s (%s) with SMTP\r\n", s.srv.Hostname, s.srv.AppName))
buffer.WriteString(fmt.Sprintf(" for <%s>; %s\r\n", to[0], now))
fmt.Fprintf(&buffer, "Received: from %s (%s [%s])\r\n", s.remoteName, s.remoteHost, s.remoteIP)
fmt.Fprintf(&buffer, " by %s (%s) with SMTP\r\n", s.srv.Hostname, s.srv.AppName)
fmt.Fprintf(&buffer, " for <%s>; %s\r\n", to[0], now)
return buffer.Bytes()
}