邮箱发送连接激活注册账号2021-03-22

一 通过Composer安装phpmailer

composer require phpmailer/phpmailer

二 common.php公共函数文件

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


function mailer($email, $title, $body)
{
    set_time_limit(0);                                     //如果上传附件卡,将脚本执行限制时间修改为0
    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->CharSet = 'utf-8';                                     //防止中文乱码
        $mail->SMTPDebug = SMTP::DEBUG_OFF;                         //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host = 'smtp.qq.com';                                //Set the SMTP server to send through
        $mail->SMTPAuth = true;                                     //Enable SMTP authentication
        $mail->Username = '47083629745@qq.com';                //SMTP username
        $mail->Password = 'zfxxwqssu1212121';                       //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port = 465;                                          //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom('47083629745@qq.com', '111的测试邮箱');
        $mail->addAddress($email);                              //Add a recipient
//        $mail->addAddress('ellen@example.com');               //Name is optional
//        $mail->addReplyTo('info@example.com', 'Information');
//        $mail->addCC('cc@example.com');
//        $mail->addBCC('bcc@example.com');

        //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 = $title;
        $mail->Body = $body;
//        $mail->AltBody = ''
        
        $mail->send();
        echo '邮箱发送成功';
    } catch (Exception $e) {
        echo "邮箱发送失败: {$mail->ErrorInfo}";
    }
}

三Controller 控制器调用 mailer()

    /**
     * @return string
     * 邮箱链接点击激活注册
     */
    public function register()
    {
        //两个逻辑
        if (Request::isPost()) {
            //获取表单数据
            $username = input('username');
            $password = input('password');
            $repassword = input('repassword');
            $email = input('email');
            //整理数据
            $data = [
                'username' => $username,
                'password' => $password,
                'repassword' => $repassword,
                'email' => $email,
                'validate_key' => md5(mt_rand(10000, 99999) . $username . $password),   //激活识别码
            ];
            //验证数据
            $validate = new AdminValidate();
            if (!$validate->scene('register')->check($data)) {
                return $validate->getError();
            }
            //插入数据库
            $model = new Admin();
            $res = $model->allowField(true)->save($data);
            //获取刚刚插入的自增id
            $id = $model->id;
            //判断注册结果
            if (!$res) return '注册失败,请重试~';
            //激活链接
            $active_url = 'http://whr.blog.ii/admin/login/register?id=' . $id . '&validate_key=' . $data['validate_key'];
            //发送邮件
            mailer($data['email'],
                '激活邮箱(吴昊然测试)',
                '恭喜您,注册成功!请点击链接激活您的帐户:<a href="' . $active_url . '" target="_blank">请点击激活</a>,如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。');
            exit();

        } else {

            $id = input('id');
            $validate_key = input('validate_key');
            if (empty($id) || empty($validate_key)) return '参数不能为空';
            $model = new Admin();
            $res = $model::where($id)->find();
            if ($res !== null) {
                $status = $model->save(['status' => 1], $id);
                if (!$status) {
                    return '激活失败';
                }
                return '激活成功,赶快去登录吧';
            } else {
                return '请重新注册';
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容