发送邮箱

使用composer安装phpmailer

composer require phpmailer/phpmailer

方法:

//发送邮箱
    public function getEmail(){

        $data = $_POST;

        require dirname(dirname(dirname(__DIR__))).'/vendor/autoload.php';

        // 随机因子
        $arr = [1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'];
        // 通过循环得到四位验证码

        $str = '0';

        for($i = 0;$i < 6;$i++){
            $key = array_rand($arr);
            $str .= $arr[$key];
        }

        $str = substr($str,1);  //自定义随机验证码

        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

        try {
            //Server settings
//            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            //smtp登录的账号 这里填入字符串格式的qq号即可
            $mail->Username = '[email protected]';                 // SMTP username
            //smtp登录的密码 使用生成的授权码(通过qq邮箱活得的授权码)
            $mail->Password = '****';                           // SMTP password
            //设置使用ssl加密方式登录鉴权
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
            $mail->Port = 465;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom('[email protected]', 'Mailer');
            //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
            // 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
            $mail->addAddress('163.com', 'Joe User');     // Add a recipient
            //$mail->addAddress('[email protected]');               // Name is optional
            //答复
            $mail->addReplyTo('[email protected]', 'Information');
            //$mail->addCC('[email protected]');
            //$mail->addBCC('[email protected]');

            //Attachments 附件
            //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = '标题';
            $mail->Body    = '内容‘</b>';
            $mail->AltBody = '内容';

            $mail->send();

            return json_encode(true);die();
            echo 'Message has been sent';
        } catch (Exception $e) {

            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }

猜你喜欢

转载自www.cnblogs.com/dream-meng/p/12640173.html
今日推荐