wordpress 本身是可以进行对评论用户的邮件回复的,但其默认使用的是 mail() 函数,如果空间商不给打开 mail() 函数的话就没有办法将回复的内容发送到访客的邮箱,访客也就不能第一时间看到站点发送的邮件了。,除了评论回复邮件通知外,需要使用邮件的还有找回密码,注册会员等,如果空间没有开启 mail() 函数,这些功能都是无法使用的,下面夏日博客就来教大家一种使用纯代码来实现评论回复邮件通知的功能。,首先需要让 wordpress 支持 SMTP,因为我们要使用这种方式来进行邮件的发送,代码如下:,代码中的注释都已经写好了,在使用 SMTP 发送邮件的时候要先把 SMTP 服务器需要设置的参数设置成自己的,如果不会设置的话直接到相应邮件商邮件设置里面去查看,一般都有详细的说明的,把这段代码放入到主题 functions.php 文件中,这个时候 wordpress 已经支持 SMTP 发送邮件了,我们还需要进行邮件的发送,让 wordpress 评论后进行自动邮件的回复吧,代码如下:,

wordpress 本身是可以进行对评论用户的邮件回复的,但其默认使用的是 mail() 函数,如果空间商不给打开 mail() 函数的话就没有办法将回复的内容发送到访客的邮箱,访客也就不能第一时间看到站点发送的邮件了。

除了评论回复邮件通知外,需要使用邮件的还有找回密码,注册会员等,如果空间没有开启 mail() 函数,这些功能都是无法使用的,下面夏日博客就来教大家一种使用纯代码来实现评论回复邮件通知的功能。

首先需要让 wordpress 支持 SMTP,因为我们要使用这种方式来进行邮件的发送,代码如下:

//smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '夏日博客(xiariboke)'; //发信人名称
$phpmailer->Host = 'smtp.exmail.qq.com'; //smtp服务器
$phpmailer->Port = 465;  //端口
$phpmailer->Username = 'admin@xiariboke.net';//邮箱帐号  
$phpmailer->Password = '********';//邮箱密码
$phpmailer->From = 'admin@xiariboke.net'; //邮箱帐号
$phpmailer->SMTPAuth = true;  
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl)
$phpmailer->IsSMTP();
}

代码中的注释都已经写好了,在使用 SMTP 发送邮件的时候要先把 SMTP 服务器需要设置的参数设置成自己的,如果不会设置的话直接到相应邮件商邮件设置里面去查看,一般都有详细的说明的,把这段代码放入到主题 functions.php 文件中,这个时候 wordpress 已经支持 SMTP 发送邮件了,我们还需要进行邮件的发送,让 wordpress 评论后进行自动邮件的回复吧,代码如下:

//评论回复邮件
function comment_mail_notify($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')){
    $wp_email = 'webmaster@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '你在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '
    
    留言回复通知 | xiariboke(夏日博客)
  ' . trim(get_comment($parent_id)->comment_author) . ', 你好!

  你曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
  --->'
. trim(get_comment($parent_id)->comment_content) . '

   ' . trim($comment->comment_author) . ' 给你的回复:
  --->'
. trim($comment->comment_content) . '

  你可以点击 查看回复完整内容

  感谢你对 ' . get_option('blogname') . ' 的关注,欢迎订阅本站

   更多内容请:夏日博客 | 联系站长 | 投稿 | 投稿须知 | 关于我们 | 联系我们

'; $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 ); } } add_action('comment_post', 'comment_mail_notify');

,

wordpress 本身是可以进行对评论用户的邮件回复的,但其默认使用的是 mail() 函数,如果空间商不给打开 mail() 函数的话就没有办法将回复的内容发送到访客的邮箱,访客也就不能第一时间看到站点发送的邮件了。

除了评论回复邮件通知外,需要使用邮件的还有找回密码,注册会员等,如果空间没有开启 mail() 函数,这些功能都是无法使用的,下面夏日博客就来教大家一种使用纯代码来实现评论回复邮件通知的功能。

首先需要让 wordpress 支持 SMTP,因为我们要使用这种方式来进行邮件的发送,代码如下:

//smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '夏日博客(xiariboke)'; //发信人名称
$phpmailer->Host = 'smtp.exmail.qq.com'; //smtp服务器
$phpmailer->Port = 465;  //端口
$phpmailer->Username = 'admin@xiariboke.net';//邮箱帐号  
$phpmailer->Password = '********';//邮箱密码
$phpmailer->From = 'admin@xiariboke.net'; //邮箱帐号
$phpmailer->SMTPAuth = true;  
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl)
$phpmailer->IsSMTP();
}

代码中的注释都已经写好了,在使用 SMTP 发送邮件的时候要先把 SMTP 服务器需要设置的参数设置成自己的,如果不会设置的话直接到相应邮件商邮件设置里面去查看,一般都有详细的说明的,把这段代码放入到主题 functions.php 文件中,这个时候 wordpress 已经支持 SMTP 发送邮件了,我们还需要进行邮件的发送,让 wordpress 评论后进行自动邮件的回复吧,代码如下:

//评论回复邮件
function comment_mail_notify($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')){
    $wp_email = 'webmaster@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '你在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '
    
    留言回复通知 | xiariboke(夏日博客)
  ' . trim(get_comment($parent_id)->comment_author) . ', 你好!

  你曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
  --->'
. trim(get_comment($parent_id)->comment_content) . '

   ' . trim($comment->comment_author) . ' 给你的回复:
  --->'
. trim($comment->comment_content) . '

  你可以点击 查看回复完整内容

  感谢你对 ' . get_option('blogname') . ' 的关注,欢迎订阅本站

   更多内容请:夏日博客 | 联系站长 | 投稿 | 投稿须知 | 关于我们 | 联系我们

'; $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 ); } } add_action('comment_post', 'comment_mail_notify');
最后修改:2025 年 09 月 10 日
如果觉得我的文章对你有用,请随意夸赞