Skip to content

fix: Keep raw message on smtp/mime failure#12641

Open
kesselb wants to merge 1 commit intomainfrom
store-raw-message-on-failure
Open

fix: Keep raw message on smtp/mime failure#12641
kesselb wants to merge 1 commit intomainfrom
store-raw-message-on-failure

Conversation

@kesselb
Copy link
Contributor

@kesselb kesselb commented Mar 23, 2026

I'm undecided about it.


The use case/steps to reproduce:

  • Send an email with "info@test" as recipient
  • See the message is stuck in the oc_mail_local_messages
  • Troubleshooting is hard without knowing the actual message going out (it might be something different than a weird recipient)

@kesselb
Copy link
Contributor Author

kesselb commented Mar 23, 2026

/backport to stable5.7

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves outbox troubleshooting by trying to persist the RFC822 “raw” message into mail_local_messages.raw even when SMTP/MIME sending fails, instead of only persisting raw on successful send.

Changes:

  • Import Throwable and use non-capturing catch (Throwable) for consistency.
  • On non-retriable Horde_Mime_Exception, attempt to store $mail->getRaw(false) into the LocalMessage before marking it STATUS_ERROR.
  • Keep SMTP logout failures silent using the same Throwable import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 172 to +176
if (in_array($e->getCode(), self::RETRIABLE_CODES, true)) {
$localMessage->setStatus(LocalMessage::STATUS_SMPT_SEND_FAIL);
return;
}

Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early return for retriable SMTP codes happens before the new best-effort raw capture, so messages ending up in STATUS_SMPT_SEND_FAIL still won't have raw stored for troubleshooting. If the intent is to “keep raw message on smtp/mime failure”, consider moving the raw-capture attempt before this return (or explicitly capturing raw for this status as well).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@kesselb kesselb Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point of storing the raw message for a temporary issue by the remote. That's nothing we can fix / where having the raw message would help to troubleshoot.

Comment on lines +177 to 182
try {
$localMessage->setRaw($mail->getRaw(false));
} catch (Throwable) {
// Having the raw message is nice for troubleshooting, but should not fail hard.
}
$localMessage->setStatus(LocalMessage::STATUS_ERROR);
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior now attempts to persist the raw RFC822 message when Horde_Mime_Exception occurs (non-retriable path), but there doesn't appear to be unit/integration coverage asserting that LocalMessage::getRaw() is populated (and that failures in getRaw(false) are tolerated). Adding a focused test would prevent regressions in this troubleshooting path.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕

@kesselb
Copy link
Contributor Author

kesselb commented Mar 23, 2026

lib/Service/MailTransmission.php:329:5: PossiblyInvalidArgument: Argument 3 of OCA\Mail\IMAP\MessageMapper::save expects string, but possibly different type resource|string provided

True, but that also a different method I didn't even touch 🤷

bytestream/Mime#15 should help.

@kesselb
Copy link
Contributor Author

kesselb commented Mar 23, 2026

Error: lib/Service/MailTransmission.php:244:5: PossiblyNullArgument: Argument 3 of OCA\Mail\IMAP\MessageMapper::save cannot be null, possibly null value provided (see https://psalm.dev/078)
Error: lib/Service/MailTransmission.php:330:5: PossiblyNullArgument: Argument 3 of OCA\Mail\IMAP\MessageMapper::save cannot be null, possibly null value provided (see https://psalm.dev/078)

Classic MessageMapper != MessageMapper 🤣 🙈

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
@kesselb kesselb force-pushed the store-raw-message-on-failure branch from c72e0e9 to b0aa093 Compare March 23, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants