【建站技巧】WordPress代码版,实现SMTP邮件推送、评论、密码修改等功能

由于博主使用wordpress自带SMTP有问题,所以找个各种替代办法,最终选择代码版,使用效果还不错,在这里分享一下!

WordPress实现发邮件的必备条件

WordPress通过SMTP发邮件需要开通465端口,服务器也需要开通响应端口,由于博主使用QQ邮箱,所以需要QQ邮箱的账号设置里开启SMTP。图示:

 

开启SMTP服务后再把下面的这段邮件配置代码添加到网站主题的 functions.php文件中,邮箱信息改成你自己的。

//smtp发送邮件功能,
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '爱玩资源网'; //名字
$phpmailer->From = 'admin@iwzyw.com';//邮件账号
$phpmailer->Host = 'smtp.qq.com'; //smtp地址,可以到你使用的邮件设置里面找
$phpmailer->Port = 465; //端口,一般不用修改
$phpmailer->Username = 'admin@iwzyw.com'; //邮件账号
$phpmailer->Password = 'xxxxxxx'; //邮件密码(授权码)
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true; 
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl)一般不用修改
}

WordPress开启用户注册修改密码SMTP邮件提醒功能设置教程

把网站设置的常规选项中的允许任何人注册勾选即可,如下图所示。

 

WordPress有用户留言时SMTP邮件提醒功能设置教程

WordPress后台开通有评论等待审核的时候邮件通知我,如下图所示:

 

勾选保存后再把下面的代码发到网站主题的 functions.php文件中,即可实现用户在我们的网站留言后邮件通知了。

//用户留言时邮件提醒功能,爱玩资源网(www.iwzyw.com)
function comment_mail_notify($comment_id) {
$admin_notify = '0'; // 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 = '您在 [' . 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;">
<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 style="text-decoration:none; color:#5692BC" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">这里查看回复的完整內容</a>
<p>(此邮件由' . get_option('blogname') . '系统自动发送,请勿回复.)</p>
</div>';
$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_post', 'comment_mail_notify');

//文章页提交留言处自动加勾选栏 
function add_checkbox() {
echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
}
add_action('comment_form', 'add_checkbox');

注:如有错误报错等,请检查相关端口或者功能是否开启!

原文链接:https://www.iwzyw.com/2022/04/18/743.html,转载请注明出处。
0
分享海报

评论0

请先

显示验证码
没有账号?注册  忘记密码?