fix(email): set sender before PHPMailer init #21

Merged
kate522 merged 1 commits from worktree/fix-mail-from into main 2026-09-04 05:57:57 +00:00
2 changed files with 24 additions and 8 deletions
+17 -8
View File
@@ -17,8 +17,24 @@ add_filter('show_admin_bar', '__return_false');
// SMTP 邮件配置注入 // 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) { add_action('phpmailer_init', function ($phpmailer) {
$mail = class_exists('\AKSRT\Settings\Settings') $mail = class_exists('\\AKSRT\\Settings\\Settings')
? \AKSRT\Settings\Settings::get_smtp() ? \AKSRT\Settings\Settings::get_smtp()
: []; : [];
if (empty($mail['enabled']) || empty($mail['host'])) return; 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->SMTPSecure = (string) ($mail['encryption'] ?? 'ssl');
$phpmailer->Timeout = 10; $phpmailer->Timeout = 10;
$phpmailer->Timelimit = 15; $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. // mail_title_prefix is applied by the canonical renderer and by third-party wp_mail callers.
@@ -47,7 +47,14 @@ test('email templates and SMTP use one aksrt_settings schema', () => {
}) })
test('SMTP authentication and failures use the canonical mail service', () => { 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.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, /add_action\('wp_mail_failed',\s*\$capture_failure\)/)
assert.match(mail, /remove_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 测试失败:'/) assert.match(mail, /error_log\('\[AKSRT\] SMTP 测试失败:'/)