WordPress无插件实现邮箱SMTP发邮件功能【WP系列教程之八】

想必有很多小伙伴对wordpress发件功能无效果而烦恼吧,没关系,开始折腾,大家都知道,目前发邮件有两种方式:

第一种是wp默认的mail函数方式(sendmail)发邮件,这个是wp默认的,但很多用户使用的虚拟主机都禁用了mail函数以至于网站无法发邮件(注册用户、找回密码等一些操作都需要发送邮件),

第二种方式就是较先进而又有效率的smtp方式。(我们的宗旨是能不用插件就不用插件)

教程开始:

拥有一个smtp功能的邮箱,其次,小编以QQ邮箱为演示邮箱。要把邮箱的smtp的服务打开,smtp服务设置在(设置——帐户设置——POP3/SMTP/IMAP服务 下两个全勾上),点击生成授权码。如下图:

生成的授权码替换并添加到以下代码到functions.php:

  1. add_action('phpmailer_init', 'mail_smtp');
  2. function mail_smtp( $phpmailer ) {
  3. $phpmailer->FromName = '古风网络博客'; //发件人名称
  4. $phpmailer->Host = 'smtp.qq.com'; //修改为你使用的邮箱SMTP服务器
  5. $phpmailer->Port = 465; //SMTP端口
  6. $phpmailer->Username = '273827769@qq.com'; //邮箱账户
  7. $phpmailer->Password = '*********'; //邮箱密码(此处填写QQ邮箱生成的授权码)
  8. $phpmailer->From = '273827769@qq.com'; //邮箱账户
  9. $phpmailer->SMTPAuth = true;
  10. $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25时->留空,465时->ssl)
  11. $phpmailer->IsSMTP();
  12. }

附上评论回复邮件通知代码:

/* 给WordPress集成评论回复邮件通知 */
add_action('comment_post','CommentsReplyNotification');
function CommentsReplyNotification($comment_id){
//取得插入评论的id
$c = get_comment($comment_id);
//取得评论的父级id
$comment_parent = $c->comment_parent;
//取得评论的内容
$c_content = $c->comment_content;
//评论者email
$c_author_email = $c->comment_author_email;

if($comment_parent != 0){
$pc = get_comment($comment_parent);
$comment_ID = $pc->comment_ID;
$comment_author = $pc->comment_author;
$comment_author_email = $pc->comment_author_email;
$comment_post_ID = $pc->comment_post_ID;
$comment_content = $pc->comment_content;
$ps = get_post($comment_post_ID);
$author_id = $ps->post_author;
$u_email = get_user_meta($author_id,'email',true);

//判断自己的回复,如果自己参与评论,不给自己发送邮件通知
if($c_author_email == $comment_author_email || $comment_author_email == $u_email ){
return;
}

$post_title = $ps->post_title;
$link = get_permalink($comment_post_ID);

//邮件内容,可以自定义内容
$content = "尊敬的".$comment_author."您好,你在古风网络博客发布于" ".$post_title.""的评论:rn".$comment_content."rn有了回复:rn".$c_content."rn请及时点击链接回复评论:".$link."#comment-".$comment_ID;
//发送邮件
wp_mail($comment_author_email,'评论回复:'.$post_title, $content);
}
}

在线音乐网站MKOnlinePlayer v2.32源码

上一篇

一组最新央视卫视直播源【2018.8.19】

下一篇

99%的人还看了

发表评论

插入图片

欢迎登陆本站

 | 注册

在线工具

VIP视频解析
注册

登录

忘记密码 ?