子比主题邮箱通知样式美化教程,让邮件更精致专业
很多使用子比主题搭建网站的朋友都会发现,系统默认发送的邮箱通知样式比较简陋,不仅影响用户体验,也拉低了网站整体质感。其实只需要简单修改几处代码,就能轻松实现子比主题邮箱通知样式美化,让评论通知、注册通知、找回密码等邮件变得更加美观、整洁、专业。
饺子为大家分享一套简单实用的子比主题邮箱美化方案,无需复杂插件,纯代码实现,小白也能轻松上手。
一、为什么要美化子比主题邮箱通知?
- 默认邮件样式单调,文字排版混乱
- 缺乏品牌感,不利于提升网站形象
- 在手机、电脑客户端显示效果不一致
- 美化后更符合现代 UI 审美,用户体验更佳
通过简单的样式定制,我们可以统一邮件的字体、颜色、间距、边框和按钮效果,让每一封通知都像专业平台发出的一样。
看预览图

定位:/wp-content/themes/zibll/inc/functions/
[hidecontent type="reply"]
<?php
/*
* @Author : Qinver
* @Url : zibll.com
* @Date : 2020-10-23 21:36:42
* @LastEditTime : 2025-12-29 18:48:44
* @Email : 770349780@qq.com
* @Project : Zibll子比主题
* @Description : 一款极其优雅的Wordpress主题
* @Read me : 感谢您使用子比主题,感谢您使用正版源码。
* @Remind : 使用盗版主题会存在各种未知风险。支持正版,从我做起!
*/
//邮件smtp设置
function zib_mail_smtp($phpmailer)
{
if (_pz('mail_smtps')) {
$phpmailer->IsSMTP();
$phpmailer->FromName = _pz('mail_showname');
$phpmailer->Host = _pz('mail_host', 'smtp.qq.com');
$phpmailer->Port = _pz('mail_port', '465');
$phpmailer->Username = _pz('mail_name', '88888888@qq.com');
$phpmailer->Sender = _pz('mail_name', '88888888@qq.com');
$phpmailer->Password = _pz('mail_passwd', '123456789');
$phpmailer->From = _pz('mail_name', '88888888@qq.com');
$phpmailer->SMTPAuth = _pz('mail_smtpauth', true);
$phpmailer->SMTPSecure = _pz('mail_smtpsecure', 'ssl');
}
}
add_action('phpmailer_init', 'zib_mail_smtp');
//邮件发件人名称
function zib_mail_from_name($from_name)
{
return _pz('mail_showname', get_bloginfo('name'));
}
add_filter('wp_mail_from_name', 'zib_mail_from_name');
/**
* @description: 发送邮件给网站管理员统一接口,会发送给所有超级管理员账号
* @param {*} $title
* @param {*} $message
* @return {*}
*/
function zib_mail_to_admin($title, $message)
{
$emails = zib_get_admin_user_emails();
if ($emails) {
foreach ($emails as $e) {
@wp_mail($e, $title, $message);
}
}
}
/**
* 发送邮件
* @param string|array $to 收件人邮箱地址
* @param string $title 邮件标题
* @param string $message
*/
function zib_send_email($to, $title, $message)
{
if (is_array($to)) {
//去重
$to = array_unique($to);
foreach ($to as $e) {
zib_send_email($e, $title, $message);
}
} else {
if (is_email($to) && !stristr($to, '@no')) {
@wp_mail($to, $title, $message);
}
}
}
/**邮件内容过滤器 */
add_filter('wp_mail', 'zib_get_mail_content');
function zib_get_mail_content($mail)
{
$mail = (array) $mail;
// 只修:多余空行问题
$clean_message = preg_replace('/\n+/', "\n", trim($mail['message']));
$message = !empty($clean_message) ? nl2br($clean_message) : '';
$blog_name = get_bloginfo('name');
$description = _pz('mail_description', _pz('description', wp_title('', false)));
$description = trim($description);
$logo = _pz('logo_src');
$con_more = _pz('mail_more_content');
$content = '
<style>
body,html{background:#f7f7fe!important;margin:0;padding:0}
.acg-bg{min-height:100vh;margin:0;padding:0;background:linear-gradient(120deg,#f6f7fe 0%,#fff7fd 100%);font-family:Helvetica,Arial,"PingFang SC","Microsoft YaHei",sans-serif}
.acg-main{max-width:480px;margin:36px auto;background:rgba(255,255,255,.97);border-radius:16px;box-shadow:0 8px 40px 0 rgba(163,217,255,.1),0 2px 8px 0 rgba(255,156,207,.1);border:1.5px solid #f5eafe;position:relative;z-index:1;overflow:hidden}
.acg-logo-box{text-align:center;position:relative;z-index:2;background:linear-gradient(90deg,#ffe7ee 15%,#e6f7ff 80%);padding:22px 0 7px;border-top-left-radius:16px;border-top-right-radius:16px}
.acg-logo{width:78%;min-width:110px;max-width:270px;max-height:56px;display:inline-block;object-fit:contain;background:transparent}
.acg-content{padding:0 30px 10px;position:relative;z-index:2}
.acg-divider{width:90%;height:22px;margin:12px auto 0;display:block}
.acg-divider svg{display:block;width:100%;height:22px}
.acg-message{font-size:17px;color:#332060;line-height:1.6;margin:17px 0 10px;word-break:break-all;font-weight:500}
.acg-message br{display:block;margin:8px 0}
.acg-desc{background:linear-gradient(93deg,#ffeafb 40%,#e8fbff 130%);color:#dd6eb9;font-size:15px;margin:18px 0 5px;padding:9px 15px;border-radius:10px;font-weight:500;box-shadow:0 1.5px 5px rgba(255,229,244,.2);border:1px dashed rgba(255,215,250,.4)}
.acg-more{background:#e8fbff;color:#69bbf3;font-size:15px;margin:0 0 12px;padding:8px 12px 11px 11px;border-radius:11px;font-weight:500;border:1px dashed rgba(211,235,255,.4)}
.acg-btn-area{text-align:center;padding:10px 0}
.acg-footer{text-align:center;color:#ada5ef;font-size:13px;background:linear-gradient(90deg,rgba(251,232,253,.2) 10%,rgba(227,246,255,.2) 90%);border-top:1px solid #f3e3ff;border-bottom-left-radius:16px;border-bottom-right-radius:16px;padding:13px 0 8px;letter-spacing:.5px;margin-top:-4px}
</style>
<div class="acg-bg">
<div class="acg-main">
<div class="acg-logo-box">
<img src="' . $logo . '" alt="logo" class="acg-logo">
</div>
<div class="acg-divider">
<svg viewBox="0 0 300 22" width="300" height="22" xmlns="http://www.w3.org/2000/svg"><path d="M0,19 Q50,2 150,18 Q250,28 300,7" fill="none" stroke="#e8caff" stroke-width="3.2" stroke-linecap="round"/></svg>
</div>
<div class="acg-content">
<div class="acg-message">' . $message . '</div>
' . ($description ? '<div class="acg-desc">' . $description . '</div>' : '') . '
' . ($con_more ? '<div class="acg-more">' . $con_more . '</div>' : '') . '
</div>
<div class="acg-btn-area"></div>
<div class="acg-footer">© ' . $blog_name . ' | 自动邮件,请勿直接回复</div>
</div>
</div>
';
$blogname = wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
$mail['subject'] = str_replace('[' . $blogname . ']', '', $mail['subject']);
$mail_title_prefix = _pz('mail_title_prefix');
$mail['subject'] = $mail_title_prefix . $mail['subject'];
$mail['message'] = $content;
$mail['headers'] = array('Content-Type: text/html; charset=UTF-8');
return $mail;
}
//用户收到私信之后向用户发送邮件通知
if (_pz('email_private_receive') && _pz('message_s') && _pz('private_s')) {
add_action('zib_add_message', 'zib_private_receive_email', 99);
}
function zib_private_receive_email($msg)
{
$msg = (array)$msg;
if ('private' != $msg['type']) {
return false;
}
$send_user_id = $msg['send_user'];
$receive_user_id = $msg['receive_user'];
$send_udata = get_userdata($send_user_id);
$receive_udata = get_userdata($receive_user_id);
$send_limit = _pz('email_private_receive_limit', 'all');
switch ($send_limit) {
case 'admin':
if (!is_super_admin($receive_user_id)) return false;
break;
case 'vip2':
$vip_level = zib_get_user_vip_level($receive_user_id);
if (!$vip_level || $vip_level < 2) return false;
break;
case 'vip':
$vip_level = zib_get_user_vip_level($receive_user_id);
if (!$vip_level) return false;
break;
}
if (!is_email($receive_udata->user_email) || stristr($receive_udata->user_email, '@no')) {
return false;
}
$_link = zibmsg_get_conter_url('private');
$title = '收到用户[' . $send_udata->display_name . ']发来的私信';
$message = '您好!' . $receive_udata->display_name . '<br>';
$message .= '收到一条新的私信消息<br>';
$message .= '用户:' . $send_udata->display_name . '<br>';
$message .= '内容:<br>';
$message .= '<div style="padding:10px 15px;border-radius:8px;background:rgba(141,141,141,0.05);line-height:1.7;">' . Zib_Private::get_content($msg) . '</div>';
$message .= '时间:' . $msg['create_time'] . '<br><br>';
$message .= '您可以打开下方链接查看此消息<br>';
$message .= '<a target="_blank" href="' . esc_url($_link) . '">' . $_link . '</a><br>';
@wp_mail($receive_udata->user_email, $title, $message);
}
/**用户评论通过审核之后向用户发送邮件 */
if (_pz('email_comment_approved', true)) {
add_action('comment_unapproved_to_approved', 'zib_comment_approved_email', 99);
}
function zib_comment_approved_email($comment)
{
$user_id = $comment->user_id;
$udata = get_userdata($user_id);
if (!is_email($udata->user_email) || stristr($udata->user_email, '@no')) {
return false;
}
$post_title = get_the_title($comment->comment_post_ID);
$_link = get_comment_link($comment->comment_ID);
$post_tlink = get_the_permalink($comment->comment_post_ID);
$title = '您发布的评论已通过审核';
$message = '您好!' . get_comment_author($comment->comment_ID) . '<br>';
$message .= '您在[<a href="' . esc_url($post_tlink) . '">' . $post_title . '</a>]中的评论,已经通过审核<br>';
$message .= '评论内容:<br>';
$message .= '<div style="padding:10px 15px;border-radius:8px;background:rgba(141,141,141,0.05);line-height:1.7;">' . get_comment_text($comment->comment_ID) . '</div>';
$message .= '评论时间:' . $comment->comment_date . '<br><br>';
$message .= '您可以打开下方链接查看评论<br>';
$message .= '<a target="_blank" href="' . esc_url($_link) . '">' . $_link . '</a><br>';
@wp_mail($udata->user_email, $title, $message);
}
// 文章或帖子审核通过、或驳回后,给作者发邮件
if (_pz('email_newpost_to_publish', true)) {
add_action('pending_to_publish', 'zib_email_pending_to_publish', 99);
add_action('publish_to_pending', 'zib_email_publish_to_pending', 99);
}
function zib_email_publish_to_pending($post)
{
$user_id = $post->post_author;
if (!$user_id || empty($_REQUEST['msg_s'])) return;
$msg = !empty($_REQUEST['msg']) ? trim(strip_tags($_REQUEST['msg'])) : '';
if ($post->post_type == 'post' && _pz('post_article_user', 1) == $user_id) return;
if ($user_id == get_current_user_id()) return;
$udata = get_userdata($user_id);
if (!is_email($udata->user_email) || stristr($udata->user_email, '@no')) return false;
$_link = get_permalink($post->ID);
$title = '您发布的内容已被驳回';
$message = '您好!' . $udata->display_name . '<br>';
$message .= '您发布的内容[' . zib_str_cut($post->post_title, 0, 20, '...') . '],已被驳回,当前为待审核状态<br>';
$message .= '内容摘要:<br>';
$message .= '<div style="padding:10px 15px;border-radius:8px;background:rgba(141,141,141,0.05);line-height:1.7;">' . zib_str_cut(trim(strip_tags($post->post_content)), 0, 200, '...') . '</div>';
$message .= '提交时间:' . get_the_time('Y-m-d H:i:s', $post) . '<br>';
$message .= '驳回时间:' . $post->post_modified . '<br>';
if ($msg) $message .= '驳回说明:' . $msg . '<br>';
$message .= '<br>您可以打开下方链接查看此内容<br>';
$message .= '<a target="_blank" href="' . esc_url($_link) . '">立即查看</a><br>';
@wp_mail($udata->user_email, $title, $message);
}
function zib_email_pending_to_publish($post)
{
$user_id = $post->post_author;
if (_pz('post_article_user', 1) == $user_id) return false;
if (get_post_meta($post->ID, 'pending_to_publish_email', true)) return false;
$udata = get_userdata($user_id);
if (in_array('administrator', $udata->roles) || in_array('roles', $udata->roles)) return false;
if (!is_email($udata->user_email) || stristr($udata->user_email, '@no')) return false;
$_link = get_permalink($post->ID);
$title = '您发布的内容已通过审核';
$message = '您好!' . $udata->display_name . '<br>';
$message .= '您发布的内容[' . $post->post_title . '],已经通过审核<br>';
$message .= '内容摘要:<br>';
$message .= '<div style="padding:10px 15px;border-radius:8px;background:rgba(141,141,141,0.05);line-height:1.7;">' . zib_str_cut(trim(strip_tags($post->post_content)), 0, 200, '...') . '</div>';
$message .= '提交时间:' . get_the_time('Y-m-d H:i:s', $post) . '<br>';
$message .= '审核时间:' . get_the_modified_time('Y-m-d H:i:s', $post) . '<br><br>';
$message .= '您可以打开下方链接查看此内容<br>';
$message .= '<a target="_blank" href="' . esc_url($_link) . '">' . $_link . '</a><br>';
update_post_meta($post->ID, 'pending_to_publish_email', true);
@wp_mail($udata->user_email, $title, $message);
}
/**用户提交链接向管理员发送邮件 */
if (_pz('email_links_submit_to_admin', true)) {
add_action('zib_ajax_frontend_links_submit_success', 'zib_links_submit_email_to_admin', 99);
}
function zib_links_submit_email_to_admin($data)
{
if ($data['link_visible'] === 'Y') return false;
$linkdata = array(
'link_name' => esc_attr($data['link_name']),
'link_url' => esc_url($data['link_url']),
'link_description' => !empty($data['link_description']) ? esc_attr($data['link_description']) : '无',
'link_image' => !empty($data['link_image']) ? '<img src="' . esc_attr($data['link_image']) . '" style="width:80px;height:auto;">' : '空',
);
$_link = admin_url('link-manager.php?orderby=visible&order=asc');
$title = '有新的链接提交待审核:' . $linkdata['link_name'];
$message = '网站有新的链接提交:<br>';
$message .= '链接名称:' . $linkdata['link_name'] . '<br>';
$message .= '链接地址:' . $linkdata['link_url'] . '<br>';
$message .= '链接简介:' . $linkdata['link_description'] . '<br>';
$message .= '链接Logo:' . $linkdata['link_image'] . '<br><br>';
$message .= '您可以打开下方地址以审核该链接<br>';
$message .= '<a target="_blank" href="' . esc_url($_link) . '">' . $_link . '</a><br>';
zib_mail_to_admin($title, $message);
}
[/hidecontent]

