如果你空间不支持正常邮件发送功能的(小猫的空间就是如此),需要使用smtp发送评论邮件的话,那么你就可以继续看下文的教程了。
首先,下载smtpPHP工具,把里面的PHPMailer文件夹丢在你的主题目录里面。
然后再你 functions.php 文件里面加以下代码即可,记得修改对应的邮件参数即可,强烈建议使用163邮箱,因为我用163发smtp那么久没出过什么错,需要的地方我都有注释的:
/**
* WordPress 使用 smtp 发送<a href="https://www.wpdaxue.com/tag/%e8%af%84%e8%ae%ba%e6%8f%90%e9%86%92" title="查看与【评论提醒】相关的文章" target="_blank" rel="noopener">评论提醒</a>邮件
* https://www.wpdaxue.com/wordpress-comment-mail-notify-smtp.html
*/
function comment_mail_notify_editFromClmao($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回应:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回应内容</a></p>
<p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此邮件由系统发出, 请勿回复.)</p>
</div>';
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require get_template_directory().'/PHPMailer/class.phpmailer.php';
try {
$mail = new PHPMailer(true);
$mail->Is<a href="https://www.wpdaxue.com/tag/smtp" title="查看与【SMTP】相关的文章" target="_blank" rel="noopener">SMTP</a>();
$mail->CharSet='UTF-8';
$mail-><a href="https://www.wpdaxue.com/tag/smtp" title="查看与【SMTP】相关的文章" target="_blank" rel="noopener">SMTP</a>Auth = true;
$mail->Port = 25;
$mail->Host = "smtp.163.com";//邮箱smtp地址,此处以163为例
$mail->Username = "你的邮箱账号";//你的邮箱账号
$mail->Password = "你的邮箱密码";//你的邮箱密码
$mail->From = "你的邮箱账号";//你的邮箱账号
$mail->FromName = get_option('blogname');
$to = $to;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 80;
//$mail->AddAttachment("f:/test.png"); //可以添加附件
$mail->IsHTML(true);
$mail->Send();
} catch (phpmailerException $e) {
// echo "邮件发送失败:".$e->errorMessage(); //测试的时候可以去掉此行的注释
}
}
}
add_action('comment_post', 'comment_mail_notify_editFromClmao'); |
/**
* WordPress 使用 smtp 发送<a href="https://www.wpdaxue.com/tag/%e8%af%84%e8%ae%ba%e6%8f%90%e9%86%92" title="查看与【评论提醒】相关的文章" target="_blank" rel="noopener">评论提醒</a>邮件
* https://www.wpdaxue.com/wordpress-comment-mail-notify-smtp.html
*/
function comment_mail_notify_editFromClmao($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回应:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回应内容</a></p>
<p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此邮件由系统发出, 请勿回复.)</p>
</div>';
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require get_template_directory().'/PHPMailer/class.phpmailer.php';
try {
$mail = new PHPMailer(true);
$mail->Is<a href="https://www.wpdaxue.com/tag/smtp" title="查看与【SMTP】相关的文章" target="_blank" rel="noopener">SMTP</a>();
$mail->CharSet='UTF-8';
$mail-><a href="https://www.wpdaxue.com/tag/smtp" title="查看与【SMTP】相关的文章" target="_blank" rel="noopener">SMTP</a>Auth = true;
$mail->Port = 25;
$mail->Host = "smtp.163.com";//邮箱smtp地址,此处以163为例
$mail->Username = "你的邮箱账号";//你的邮箱账号
$mail->Password = "你的邮箱密码";//你的邮箱密码
$mail->From = "你的邮箱账号";//你的邮箱账号
$mail->FromName = get_option('blogname');
$to = $to;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 80;
//$mail->AddAttachment("f:/test.png"); //可以添加附件
$mail->IsHTML(true);
$mail->Send();
} catch (phpmailerException $e) {
// echo "邮件发送失败:".$e->errorMessage(); //测试的时候可以去掉此行的注释
}
}
}
add_action('comment_post', 'comment_mail_notify_editFromClmao');
效果图:

来源:
https://www.wpdaxue.com/wordpress-comment-mail-notify-smtp.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32217.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57