Make WordPress Core

Opened 5 months ago

Last modified 3 months ago

#64940 new enhancement

PHP mailer Exception Handler

Reported by: kajori Owned by:
Priority: normal Milestone: Awaiting Review
Component: Mail Version:
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Wrap the phpmailer_init hook (do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) )) within a try-catch block in pluggable.php to ensure that exceptions during mail initialization are properly caught and logged.

Currently, if an exception occurs during the execution of the phpmailer_init hook (e.g., due to file permission issues when accessing attachments or embedded resources), the failure is not clearly logged, making debugging difficult. In a real-world scenario, a failure occurred due to restricted access to an uploads directory, but the root cause was not visible until the hook was wrapped in an exception block.

By handling exceptions at this point, errors such as "wp_mail failed" or file access issues (e.g., inability to access /wp-content/uploads) can be properly surfaced and logged, improving debuggability and reliability of the mail system.

Change History (4)

#1 @westonruter
5 months ago

  • Focuses performance coding-standards removed

This ticket was mentioned in PR #11344 on WordPress/wordpress-develop by Kajori-Biswas.


5 months ago
#2

  • Keywords has-patch added

This PR wraps the phpmailer_init hook (do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) )) in a try-catch block within wp_mail() to ensure that exceptions during mail initialization are properly caught and logged.

Currently, if an exception occurs during the execution of the phpmailer_init hook, it may fail silently or without sufficient logging, making it difficult to identify the root cause of mail delivery issues.

In real-world scenarios, failures can occur due to issues such as:

File permission errors (e.g., inaccessible files in wp-content/uploads)
Invalid attachments or embedded resources
Third-party hook implementations throwing exceptions

These failures can result in generic errors like "wp_mail failed" without exposing the underlying cause.

Solution:
Wrapped the phpmailer_init hook execution in a try-catch block
Ensured that any thrown exceptions are properly captured
Allows meaningful error messages to be logged for debugging purposes

Trac ticket: #64940

## Use of AI Tools

#3 @audrasjb
3 months ago

Removing trunk version as this is not going to be shipped with WP 7.0 but in the next releases.

#4 @desrosj
3 months ago

  • Version trunk

Since this is an enhancement, there's no first version of WordPress this can be reproduced in. Removing trunk version.

Last edited 3 months ago by desrosj (previous) (diff)
Note: See TracTickets for help on using tickets.