fix(email): complete mail settings and templates

This commit is contained in:
kate522 committed 2026-09-04 12:06:42 +08:00
1 parent 4f8ab94fac
commit 4833053a5c
29 files changed
+716 -426

No files matched your search

+23 -33
View File
@@ -18,50 +18,40 @@ add_filter('show_admin_bar', '__return_false');
// ============================================================
add_action('phpmailer_init', function ($phpmailer) {
$mail = get_option('aksrt_mail_smtp', array());
$mail = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_smtp()
: [];
if (empty($mail['enabled']) || empty($mail['host'])) return;
$phpmailer->isSMTP();
$phpmailer->Host = $mail['host'];
$phpmailer->Port = (int) (isset($mail['port']) ? $mail['port'] : 465);
$phpmailer->SMTPAuth = !empty($mail['username']);
$phpmailer->Username = $mail['username'];
$phpmailer->Password = $mail['password'];
$phpmailer->SMTPSecure = isset($mail['encryption']) ? $mail['encryption'] : 'ssl';
$phpmailer->Port = (int) ($mail['port'] ?? 465);
$phpmailer->SMTPAuth = !empty($mail['auth']) && !empty($mail['username']);
$phpmailer->Username = (string) ($mail['username'] ?? '');
$phpmailer->Password = (string) ($mail['password'] ?? '');
$phpmailer->SMTPSecure = (string) ($mail['encryption'] ?? 'ssl');
$phpmailer->Timeout = 10;
$phpmailer->Timelimit = 15;
// 性能优化:超时 + 连接保持
$phpmailer->Timeout = 10; // 连接/读取超时 10 秒
$phpmailer->Timelimit = 15; // 总执行上限 15 秒
if (!empty($mail['from_address'])) {
$from_name = isset($mail['from_name']) ? $mail['from_name'] : '';
// mail_showname 覆盖 from_name
if (class_exists('\AKSRT\Settings\Settings')) {
$branding = \AKSRT\Settings\Settings::get_mail_branding();
if ($branding['showname'] !== '') {
$from_name = $branding['showname'];
}
}
$phpmailer->setFrom($mail['from_address'], $from_name);
}
if (!empty($mail['from_name'])) {
$phpmailer->FromName = $mail['from_name'];
}
// mail_showname 全局覆盖 FromName
if (class_exists('\AKSRT\Settings\Settings')) {
$branding = \AKSRT\Settings\Settings::get_mail_branding();
if ($branding['showname'] !== '') {
$phpmailer->FromName = $branding['showname'];
}
$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:为所有邮件主题添加前缀
// mail_title_prefix is applied by the canonical renderer and by third-party wp_mail callers.
add_filter('wp_mail', function ($atts) {
if (!class_exists('\AKSRT\Settings\Settings') || empty($atts['subject'])) return $atts;
$branding = \AKSRT\Settings\Settings::get_mail_branding();
if ($branding['title_prefix'] !== '' && strpos($atts['subject'], $branding['title_prefix']) !== 0) {
$atts['subject'] = $branding['title_prefix'] . ' ' . $atts['subject'];
$subject = (string) $atts['subject'];
$site = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = preg_replace('/^\[' . preg_quote($site, '/') . '\]\s*/u', '', $subject);
if ($branding['title_prefix'] !== '' && strpos($subject, $branding['title_prefix']) !== 0) {
$atts['subject'] = $branding['title_prefix'] . ' ' . $subject;
} else {
$atts['subject'] = $subject;
}
return $atts;
});