zoukankan      html  css  js  c++  java
  • WordPress无插件实现SMTP给评论用户发送邮件提醒

    wordpress中集成PHPMalier给评论用户发送邮件提醒

    首先你得去下载PHPMalier

     注:PHPMailer需PHP的socket扩展支持。如果PHPMailer连接邮箱需要ssl加密方式的话,php还得openssl的支持,可以查看phpinfo,如下两项均存在则可以使用。

    PHP环境下,是提供了发送邮件的函数mail()的,不过该函数要求服务器支持sendmail或者必须设置一台不需要中继的邮件发送服务器,但现在要找到一台不需要身份验证的邮件发送中继几乎不可能,所以使用mail函数往往无法成功发送电子邮件。那么php就无法发送邮件了么?答案是否定的,自己如果熟悉POP3/SMTP协议,结合socket功能就可以编写高效稳定的邮件发送程序,但这种变通方法相当于使用php编写了一个POP3/SMTP客户端了,对一般用户来说尚有难度。
    好在互联网上的资源非常多,于是PHPMailer映入眼帘。

    PHPMailer的优点

    可运行在任何平台之上
    支持SMTP验证
    发送邮时指定多个收件人,抄送地址,暗送地址和回复地址;注:添加抄送、暗送仅win平台下smtp方式支持
    支持多种邮件编码包括:8bit,base64,binary和quoted-printable
    支持冗余SMTP服务器,即可以指定主smtp服务器地址也只可以指定备份smtp服务器
    支持带附件的邮件,可以为邮件添加任意格式的附件---当然得你的服务器有足够大的带宽支撑
    自定义邮件头信息,这跟php中通过header函数发送头信息类似
    支持将邮件正文制作成HTMl内容,那么就可以在邮件正文中插入图片
    灵活的debug支持
    经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等

    下载下来的PHPMailer解压后进行瘦身,仅需要class.phpmailer.php、class.pop3.php、class.smtp.php以及PHPMailerAutoload.php四个文件,language文件夹可要可不要,那个主要用于debug调试时的显示信息。看下class.phpmailer.php的文件大小,110kb多,挺吓人的。

    如果你使用的smtp是ssl连接的话,请参考:《使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"

    在自己主题下的function.php文件中添加以下代码:

    require_once (TEMPLATEPATH.'/PHPMailer/PHPMailerAutoload.php'); 
    
    function wp_comment_mail_notify($comment_id)
    {
        $ishtml = true;
        $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 發出點, no-reply 可改為可用的 e-mail.
            $to = trim(get_comment($parent_id)->comment_author_email);
            $subject = '你在 ' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) .' 的留言有了新回复';
            $message = '
                <div style="background: #F1F1F1; 100%;padding: 50px 0;">
                    <div style="background: #FFF; 750px;margin: 0 auto;">
                        <div style="padding: 10px 60px;background: #50A5E6;color: #FFF;font-size: 24px; font-weight: bold;"><a href="' . get_option('home') . '" style="text-decoration: none;color: #FFF;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</a></div>
                        <h1 style="text-align: center;font-size: 26px;line-height: 50px;margin: 30px 60px;font-weight: bold;font-family: 宋体,微软雅黑,serif;">
                            你在 [' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '] 的留言有了新回复
                        </h1>
                        <div style="border-bottom: 1px solid #333;height: 0px;margin: 0 60px;"></div>
                        <div style="margin: 30px 60px;color: #363636;">
                            <p style="font-size: 16px;font-weight: bold;line-height: 30px;">Hi,' . trim(get_comment($parent_id)->comment_author) . '!</p>
                            <div style="font-size: 16px;">
                                <p><strong>你曾在本博客《' . get_the_title($comment->comment_post_ID) . '》的留言为:</strong></p>
                                <blockquote style="border-left: 4px solid #ddd; padding: 5px 10px; line-height: 22px;">' . trim(get_comment($parent_id)->comment_content) . '</blockquote>
                            </div>
                            <div style="font-size: 16px;">
                                <p><strong>' . trim($comment->comment_author) . ' 给你的回复为:</strong></p>
                                <blockquote style="border-left: 4px solid #ddd; padding: 5px 10px; line-height: 22px;">' . trim($comment->comment_content) . ' </blockquote>
                            </div>
                            <p style="font-size: 16px;line-height: 30px;">
                                你可以点击此链接 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '" style="text-decoration: none;color: #50A5E6;">查看完整回复内容</a> | 欢迎再次来访 <a href="' . get_option('home') . '" style="text-decoration: none;color: #50A5E6;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</a>
                            </p>
                            <p style="color: #999;">(此邮件由系统自动发出,请勿回复!)</p>
                        </div>
                        <div style="border-bottom: 1px solid #dfdfdf;height: 0px;margin: 0 60px;"></div>
                        <div style="text-align: right;padding: 30px 60px;color: #999;">
                            <p>Powered by ' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) .'</p>
                        </div>
                    </div>
                </div>';
            wp_php_mail ($to,$subject,$message,$ishtml,$nohtmlmess='');
        }
    }
    add_action('comment_post', 'wp_comment_mail_notify');
    
    //基于PHPMailer的邮件发送
    function wp_php_mail ($to,$subject,$message,$ishtml,$nohtmlmess='')
    {
        $mail = new PHPMailer;
    
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
    
        //$mail->SMTPDebug = 2;                               // Enable verbose debug output
    
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = PM_HOST;  // Specify main and backup SMTP servers
        $mail->SMTPAuth = PM_SMTPAuth;                               // Enable SMTP authentication
        $mail->Username = PM_Username;                 // SMTP username
        $mail->Password = PM_Password;                           // SMTP password
        $mail->SMTPSecure = PM_SMTPSecure;                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = PM_Port;                                    // TCP port to connect to
        $mail->CharSet = PM_CharSet;
    
        $mail->setFrom(PM_Username, PM_Username);
        $mail->addAddress($to, $to);     // Add a recipient
        $mail->addReplyTo(PM_Username, 'Information');
        $mail->isHTML($ishtml);                                  // Set email format to HTML
    
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->AltBody = $nohtmlmess;
    
        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
        }
    }
    
    /**
     * 修改邮件发送方式为使用SMTP发送
     */
    add_action('phpmailer_init', 'mail_smtp');
    function mail_smtp( $phpmailer ) {
        $phpmailer->FromName = 'Your Name';  //对方收到邮件后显示的发件人名称
        $phpmailer->Host = PM_HOST;  //SMTP服务器
        $phpmailer->Port = PM_Port;   //SMTP端口号
        $phpmailer->Username = PM_Username;  //发送邮件的邮箱账号
        $phpmailer->Password = PM_Password;  //发送邮件的邮箱密码
        $phpmailer->From = PM_Username;   //来自,应与 Username相同
        $phpmailer->SMTPAuth = PM_SMTPAuth;
        $phpmailer->SMTPSecure = PM_SMTPSecure; //tls or ssl ,是否启用SSL
        $phpmailer->IsSMTP();  //这一行指定了使用SMTP发送邮件
        return $phpmailer;
    }

    关于配置里那些常量,我写在 wp-config.php里了。关于配置该怎么写的问题,请参考这里 (直接看人家的 examples)。

  • 相关阅读:
    Eclipse使用之将Git项目转为Maven项目, ( 注意: 最后没有pom.xml文件的, 要转化下 )
    静态类型语言,动态类型语言的区别
    UT, FT ,E2E 测试的意思
    git tag 用法 功能作用
    java 常用异常及作用
    git checkout -b 分支name 分支的新建, 切换, 删除, 查看
    项目上有红色感叹号, 一般就是依赖包有问题, remove依赖,重新加载,maven的也行可认删除,自己也会得新加载
    密码学初级教程(五)消息认证码MAC-Message Authentication Code
    密码学初级教程(六)数字签名 Digital Signature
    密码学初级教程(四)单向散列函数-指纹-
  • 原文地址:https://www.cnblogs.com/wpjamer/p/7421540.html
Copyright © 2011-2022 走看看