wordpress评论回复自动邮件提醒

转自 https://wpbom.com/2499.html

我只是改了改邮件内容样式

SMTP邮件 功能在WordPress 中是很常用的功能, 虽然WordPress自带了mail函数,但用自带mail函数发送邮件很容易失败或者被拒收。

有很多虚拟主机基本都是禁用了 mail 函数,所以我们只能使用SMTP服务发送邮件,网上关于SMTP的WordPress插件有很多,今天我分享一种不需要插件纯代码实现STMP邮件发送的功能。

首先将下面代码修改后复制到functions.PHP文件,再测试发送邮件功能。


//评论回复邮件

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 = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
//发件人e-mail地址

$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
$content='
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body style="width:100%;height:800px;background-color:#EEF3FA; font-size:14px;font-family:Microsoft YaHei;">
    <div style="margin:100px auto;background-color:#fff;  width:866px; border:1px solid #F1F0F0;box-shadow: 0 0 5px #F1F0F0;">
    <div style="width:838px;height: 78px; padding-top: 10px;padding-left:28px; background-color:#F7F7F7;">
        <a style="cursor:pointer; font-size:30px; color:#333;text-decoration: none; font-weight: bold;"
           href="'. get_option('home') .'">' . get_option('blogname') . '</a><span
            style="color:#999; font-size:14px;padding-left:20px;">一个技术型主机测评网站</span>
    </div>
    <div style="padding:30px;">
        <div style="height:50px; line-height:50px; font-size:16px; color:#9e9e9e;">你有一条新的评论</div>
        <div style="line-height:30px;  font-size:16px; margin-bottom:20px; text-indent: 2em;">
            您在<a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 上的留言有回复啦!
        </div>
        <div style="line-height:40px;  font-size:14px;">
            <label style="color:#999;">评论人:</label>
            <span style="color:#333;">'.trim($comment->comment_author).' </span>
        </div>
        <div style="line-height:40px;  font-size:14px;">
            <label style="color:#999;">评论地址:</label>
            <a href="'.htmlspecialchars(get_comment_link($parent_id)).'" style="color:#333;">'.htmlspecialchars(get_comment_link($parent_id)).'</a>
        </div>
        <div style="line-height:40px;  font-size:14px;">
            <label style="color:#999;">评论时间:</label>
            <span style="color:#333;">'.trim($comment->comment_date).'</span>
        </div>
        <div style="line-height:40px;  font-size:14px;">
            <label style="color:#999;">PS:</label>
            <span style="color:#333;">请不要回复此邮件,此邮件来自博客自动发送</span>
        </div>
    </div>
</div>
</body>
</html>
';

$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $content, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing

}
}
add_action('comment_post', 'comment_mail_notify');
赞(0) 打赏
未经允许不得转载:传奇私服 - 传奇私服发布网,最好玩、最新潮的传奇SF尽在眼前 » wordpress评论回复自动邮件提醒

评论 10

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址