WordPress 默认的评论模板中是带有邮件通知的功能的,但我们使用的主题一般是开发者写的,假如不喜欢邮件模板怎么修改?下面讲一讲 WordPress 的邮件通知实现过程,想修改邮件模板也很简单。
- //评论回应邮件通知
- add_action('comment_post', '_comment_mail_notify');
- function _comment_mail_notify($comment_id) {
- $admin_notify = '1';// admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
- $admin_email = get_bloginfo('admin_email');// $admin_email 可改为你指定的 e-mail.
- $comment = get_comment($comment_id);
- $comment_author_email = trim($comment->comment_author_email);
- $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
- global $wpdb;
- if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '') {
- $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
- }
- if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1')) {
- $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
- }
- $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
- $spam_confirmed = $comment->comment_approved;
- if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));// e-mail 发出点, no-reply 可改为可用的 e-mail.
- $to = trim(get_comment($parent_id)->comment_author_email);
- $subject = 'Hi,您在 [' . get_option("blogname") . '] 的留言有人回复啦!';
- $letter = (object) array(
- 'author' => trim(get_comment($parent_id)->comment_author),
- 'post' => get_the_title($comment->comment_post_ID),
- 'comment' => trim(get_comment($parent_id)->comment_content),
- 'replyer' => trim($comment->comment_author),
- 'reply' => trim($comment->comment_content),
- 'link' => HTMLspecialchars(get_comment_link($parent_id)),
- 'sitename' => get_option('blogname')
- );
- $message = '
- <table width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse"><tbody><tr><td><table width="600" cellpadding="0" cellspacing="0" border="0" align="center" style="border-collapse:collapse"><tbody><tr><td><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td width="73" align="left" valign="top" style="border-top:1px solid #d9d9d9;border-left:1px solid #d9d9d9;border-radius:5px 0 0 0"></td><td valign="top" style="border-top:1px solid #d9d9d9"><div style="font-size:14px;line-height:10px"><br><br><br><br></div><div style="font-size:18px;line-height:18px;color:#444;font-family:Microsoft Yahei">Hi, ' . $letter->author . '<br><br><br></div><div style="font-size:14px;line-height:22px;color:#444;font-weight:bold;font-family:Microsoft Yahei">您在' . $letter->sitename . '《' . $letter->post . '》的评论:</div><div style="font-size:14px;line-height:10px"><br></div><div style="font-size:14px;line-height:22px;color:#666;font-family:Microsoft Yahei"> ' . $letter->comment . '</div><div style="font-size:14px;line-height:10px"><br><br></div><div style="font-size:14px;line-height:22px;color:#5DB408;font-weight:bold;font-family:Microsoft Yahei">' . $letter->replyer . ' 回复您:</div><div style="font-size:14px;line-height:10px"><br></div><div style="font-size:14px;line-height:22px;color:#666;font-family:Microsoft Yahei"> ' . $letter->reply . '</div><div style="font-size:14px;line-height:10px"><br><br><br><br></div><div style="text-align:center"><a href="' . $letter->link . '" target="_blank" style="text-decoration:none;color:#fff;display:inline-block;line-height:44px;font-size:18px;background-color:#61B3E6;border-radius:3px;font-family:Microsoft Yahei"> 点击查看回复 </a><br><br></div></td><td width="65" align="left" valign="top" style="border-top:1px solid #d9d9d9;border-right:1px solid #d9d9d9;border-radius:0 5px 0 0"></td></tr><tr><td style="border-left:1px solid #d9d9d9"> </td><td align="left" valign="top" style="color:#999"><div style="font-size:8px;line-height:14px"><br><br></div><div style="min-height:1px;font-size:1px;line-height:1px;background-color:#e0e0e0"> </div><div style="font-size:12px;line-height:20px;width:425px;font-family:Microsoft Yahei"><br><a href="' . _hui('letter_link_1') . '" target="_blank" style="text-decoration:underline;color:#61B3E6;font-family:Microsoft Yahei" rel="noopener noreferrer">' . _hui('letter_text_1') . '</a><br><a href="' . _hui('letter_link_2') . '" target="_blank" style="text-decoration:underline;color:#61B3E6;font-family:Microsoft Yahei" rel="noopener noreferrer">' . _hui('letter_text_2') . '</a><br>此邮件由' . $letter->sitename . '系统自动发出,请勿回复!</div></td><td style="border-right:1px solid #d9d9d9"> </td></tr><tr><td colspan="3" style="border-bottom:1px solid #d9d9d9;border-right:1px solid #d9d9d9;border-left:1px solid #d9d9d9;border-radius:0 0 5px 5px"><div style="min-height:42px;font-size:42px;line-height:42px"> </div></td></tr></tbody></table></td></tr><tr><td><div style="min-height:42px;font-size:42px;line-height:42px"> </div></td></tr></tbody></table></td></tr></tbody></table>';
- $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
- $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
- wp_mail($to, $subject, $message, $headers);
- //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
- }
- }
- //自动勾选
- add_action('comment_form', '_comment_add_checkbox');
- function _comment_add_checkbox() {
- echo '<label for="comment_mail_notify" class="checkbox inline hide" style="padding-top:0"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked"/>有人回复时邮件通知我</label>';
- }
上面的代码是 DUX 主题的评论邮件通知代码,如果你想修改邮件模板,只需修改 message 变量的内容即可,需要注意的是,邮件中虽然能用 HTML,但并不是所有的 HTML 都能用,这个就需要诸位自己去试了,或者百度查资料。
先说一下 wordpress 发送邮件是怎么工作的,首先是在提交评论的时候有一个 comment_mail_notify 参数,这个东西是控制是否评论邮件通知的。上面我们在自动勾选那一块代码中已经默认添加发送通知了。其原理是使用 comment_form 钩子修改评论框代码