PHPMailer

作者: _Damon | 来源:发表于2018-10-09 17:44 被阅读0次

    1.通过composer安装PHPMailer

    composer require phpmailer/phpmailer
    

    2.html页面

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>首页</title>
    </head>
    <body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span4">
            </div>
            <div class="span4">
                <form class="form-horizontal" method="post" action="{:url('index/index')}">
                    <div class="control-group">
                        <label class="control-label" for="inputEmail">邮箱</label>
                        <div class="controls">
                            <input id="inputEmail" type="text" name="user_email"/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="inputPassword">类容</label>
                        <div class="controls">
                            <input id="inputPassword" type="text" name="content" />
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="controls">
                            <button type="submit" class="btn">发送</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="span4">
            </div>
        </div>
    </div>
    
    </body>
    </html>
    

    3.163邮箱设置


    1539077925(1).png 1539077945(1).png

    4.PHP控制器

    <?php
    namespace app\index\controller;
    use think\Controller;
    use PHPMailer\PHPMailer\PHPMailer;
    use think\Request;
    class Index extends Controller
    {
        public function index()
        {
          if (request()->isPost()){
              $re = input('post.');
              $data = [
                  'user_email' => $re['user_email'],  //接收人邮箱
                  'content' => $re['content']
              ];
    
              $this->sendEmail($data);
          }else{
              return view();
          }
        }
    
        public  function sendEmail($data = []) {
    
            $mail = new phpmailer(); //实例化
            $mail->IsSMTP(); // 启用SMTP
            $mail->Host = 'smtp.163.com'; //SMTP服务器 以qq邮箱为例子
            $mail->Port = 465;  //邮件发送端口
            $mail->SMTPAuth = true;  //启用SMTP认证
            $mail->SMTPSecure = "ssl";   // 设置安全验证方式为ssl
            $mail->CharSet = "UTF-8"; //字符集
            $mail->Encoding = "base64"; //编码方式
            $mail->Username = '1529*****927@163.com';  //发件人邮箱
            $mail->Password = '*****';  //发件人密码 ==>重点:是{授权码}邮箱设置里面,不是邮箱密码
            $mail->Subject = '邮箱验证'; //邮件标题
            $mail->From = '152******927@163.com';  //发件人邮箱
            $mail->FromName = '测试';  //发件人姓名
            if($data && is_array($data)){
                $mail->AddAddress($data['user_email']); //添加收件人
                $mail->IsHTML(true); //支持html格式内容
                $mail->Body = $data['content']; //邮件主体内容
                //发送成功就删除
                if ($mail->Send()) {
                    //echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息,用以邮件发送不成功问题排查
                    return $this->success('发送成功');
                }else{
                    return $this->error('发送失败');
                }
    
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:PHPMailer

          本文链接:https://www.haomeiwen.com/subject/wzdyaftx.html