郵箱發(fā)送連接激活注冊賬號2021-03-22

一 通過Composer安裝phpmailer

composer require phpmailer/phpmailer

二 common.php公共函數(shù)文件

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


function mailer($email, $title, $body)
{
    set_time_limit(0);                                     //如果上傳附件卡,將腳本執(zhí)行限制時間修改為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 '郵箱發(fā)送成功';
    } catch (Exception $e) {
        echo "郵箱發(fā)送失敗: {$mail->ErrorInfo}";
    }
}

三Controller 控制器調(diào)用 mailer()

    /**
     * @return string
     * 郵箱鏈接點擊激活注冊
     */
    public function register()
    {
        //兩個邏輯
        if (Request::isPost()) {
            //獲取表單數(shù)據(jù)
            $username = input('username');
            $password = input('password');
            $repassword = input('repassword');
            $email = input('email');
            //整理數(shù)據(jù)
            $data = [
                'username' => $username,
                'password' => $password,
                'repassword' => $repassword,
                'email' => $email,
                'validate_key' => md5(mt_rand(10000, 99999) . $username . $password),   //激活識別碼
            ];
            //驗證數(shù)據(jù)
            $validate = new AdminValidate();
            if (!$validate->scene('register')->check($data)) {
                return $validate->getError();
            }
            //插入數(shù)據(jù)庫
            $model = new Admin();
            $res = $model->allowField(true)->save($data);
            //獲取剛剛插入的自增id
            $id = $model->id;
            //判斷注冊結(jié)果
            if (!$res) return '注冊失敗,請重試~';
            //激活鏈接
            $active_url = 'http://whr.blog.ii/admin/login/register?id=' . $id . '&validate_key=' . $data['validate_key'];
            //發(fā)送郵件
            mailer($data['email'],
                '激活郵箱(吳昊然測試)',
                '恭喜您,注冊成功!請點擊鏈接激活您的帳戶:<a href="' . $active_url . '" target="_blank">請點擊激活</a>,如果以上鏈接無法點擊,請將它復(fù)制到你的瀏覽器地址欄中進入訪問,該鏈接24小時內(nèi)有效。');
            exit();

        } else {

            $id = input('id');
            $validate_key = input('validate_key');
            if (empty($id) || empty($validate_key)) return '參數(shù)不能為空';
            $model = new Admin();
            $res = $model::where($id)->find();
            if ($res !== null) {
                $status = $model->save(['status' => 1], $id);
                if (!$status) {
                    return '激活失敗';
                }
                return '激活成功,趕快去登錄吧';
            } else {
                return '請重新注冊';
            }
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容