diff --git a/inc/core-hooks/part-2.php b/inc/core-hooks/part-2.php index d99d4c7c..7b495903 100644 --- a/inc/core-hooks/part-2.php +++ b/inc/core-hooks/part-2.php @@ -17,8 +17,24 @@ add_filter('show_admin_bar', '__return_false'); // SMTP 邮件配置注入 // ============================================================ +add_filter('wp_mail_from', function ($from) { + if (!class_exists('\AKSRT\Settings\Settings')) return $from; + $mail = \AKSRT\Settings\Settings::get_smtp(); + if (empty($mail['enabled'])) return $from; + $configured = sanitize_email((string) ($mail['from_address'] ?? '')); + return $configured !== '' ? $configured : $from; +}); + +add_filter('wp_mail_from_name', function ($from_name) { + if (!class_exists('\AKSRT\Settings\Settings')) return $from_name; + $mail = \AKSRT\Settings\Settings::get_smtp(); + if (empty($mail['enabled'])) return $from_name; + $configured = trim((string) ($mail['from_name'] ?? '')); + return $configured !== '' ? $configured : $from_name; +}); + add_action('phpmailer_init', function ($phpmailer) { - $mail = class_exists('\AKSRT\Settings\Settings') + $mail = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_smtp() : []; if (empty($mail['enabled']) || empty($mail['host'])) return; @@ -32,13 +48,6 @@ add_action('phpmailer_init', function ($phpmailer) { $phpmailer->SMTPSecure = (string) ($mail['encryption'] ?? 'ssl'); $phpmailer->Timeout = 10; $phpmailer->Timelimit = 15; - - $from_address = sanitize_email((string) ($mail['from_address'] ?? '')); - if ($from_address !== '') { - $from_name = (string) ($mail['from_name'] ?? get_bloginfo('name')); - $phpmailer->setFrom($from_address, $from_name); - } - $phpmailer->FromName = (string) ($mail['from_name'] ?? get_bloginfo('name')); }); // mail_title_prefix is applied by the canonical renderer and by third-party wp_mail callers. diff --git a/tests/widget-system/email-system-contract.test.cjs b/tests/widget-system/email-system-contract.test.cjs index 571c5144..5eda0327 100644 --- a/tests/widget-system/email-system-contract.test.cjs +++ b/tests/widget-system/email-system-contract.test.cjs @@ -47,7 +47,14 @@ test('email templates and SMTP use one aksrt_settings schema', () => { }) test('SMTP authentication and failures use the canonical mail service', () => { + assert.match(coreHooks, /add_filter\('wp_mail_from',/) + assert.match(coreHooks, /add_filter\('wp_mail_from_name',/) + assert.match(coreHooks, /if \(empty\(\$mail\['enabled'\]\)\) return \$from/) + assert.match(coreHooks, /if \(empty\(\$mail\['enabled'\]\)\) return \$from_name/) + assert.match(coreHooks, /\$mail\['from_address'\]/) + assert.match(coreHooks, /\$mail\['from_name'\]/) assert.match(coreHooks, /\$phpmailer->SMTPAuth\s*=\s*!empty\(\$mail\['auth'\]\)\s*&&\s*!empty\(\$mail\['username'\]\)/) + assert.doesNotMatch(coreHooks, /\$phpmailer->setFrom\(/) assert.match(mail, /add_action\('wp_mail_failed',\s*\$capture_failure\)/) assert.match(mail, /remove_action\('wp_mail_failed',\s*\$capture_failure\)/) assert.match(mail, /error_log\('\[AKSRT\] SMTP 测试失败:'/)