phpmailer綁定郵箱


1.配置php

<?php 
return array (
  'email_host' => 'smtp.aliyun.com',
  'email_port' => '25',
  'email_username' => 'diandodo@aliyun.com',
  'email_password' => 'xxxxxx',
  'email_from' => 'diandodo@aliyun.com',
  'email_fromname' => '點多多',
  'email_subject' => '助店寶商戶激活郵箱',
  'email_body' => "尊敬的用戶{$username}您好:
       您的激活碼爲<font color='red'>{$code}</font>,請將激活碼輸入進行驗證! 激活碼有效期爲6分鐘^_^",
);

2.發送函數ajax

// 發送郵件
    private function _sendEmail($email,$code,$username = '') {
        import('@.ORG.phpmailer');
        $mail = new PHPMailer(); //創建郵件發送類,類名不必定與引入的文件名相同
        $mail->CharSet = "UTF-8";
        $mail->IsSMTP(); // 使用SMTP方式發送
        $mail->Host = C('email_host'); // 您的企業郵局域名
        $mail->SMTPAuth = true; // 啓用SMTP驗證功能
        $mail->Username = C('email_username'); // 郵局用戶名(請填寫完整的email地址)
        $mail->Password = C('email_password'); // 郵局密碼
        $mail->Port=C('email_port');
        $mail->From = C('email_from'); //郵件發送者email地址
        $mail->FromName = C('email_fromname');
        $mail->AddAddress("$email", "$username");
        $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
        $mail->Subject = C('email_subject'); //郵件標題
        $email_body = "尊敬的用戶<strong>{$username}</strong>您好:
       您的激活碼爲<font color='red'>{$code}</font>,請將激活碼輸入進行驗證! 激活碼有效期爲6分鐘^_^";
        $mail->Body = $email_body; //郵件內容,上面設置HTML,則能夠是HTML

        if(!$mail->Send())
        {
            return array('status'=>2,'info'=>$mail->ErrorInfo);
        } else {
            return array('status'=>1,'info'=>'發送成功');;
        }
    }

3.生成驗證碼保存到session中,併發送session

// 發送郵箱激活碼
    public function sendActivationcode() {
        session($this->activationtime, null); 
        $activationtime = session($this->activationtime);
        $email = $this->_post('email', 'trim');
        if (IS_AJAX && (!$activationtime || time() > $activationtime)) {
            $activationcode = rand(1000, 9999);
            $res = $this->_sendEmail($email,$activationcode,$this->user['username']);
            if($res['status'] == 1) {
                //設置發送限制時間
                session($this->activationtime, time() + 50);
                session($this->activationcode, array('code' => $activationcode, 'time' => time() + 600));
                $this->ajaxReturn(array('result' => true));
            } else {
                //發送失敗寫入日誌文件
                $log = date('Y-m-d H:i:s') . " 發送失敗:{$res['info']}" . PHP_EOL;
                file_put_contents(RUNTIME_PATH . 'Log/activationcode.log', $log, FILE_APPEND);
                $this->ajaxReturn(array('result' => false, 'error' => $res['info']));
            }
        } else {
            $this->ajaxReturn(array('result' => false, 'error' => '錯誤的請求'));
        }
    }

4.驗證並綁定併發

// 綁定郵箱
    public function bind_email() {
        if (IS_POST) {
            // 獲取驗證碼
            $activationcode = $this->_post('activationcode','trim');
            $email = $this->_post('email','trim');
            $session_activationcode = session($this->activationcode);
            if (time() > $session_activationcode['time']  || $activationcode != $session_activationcode['code']) {
                $this->error('驗證碼有誤');
            } else {
                M('User')->where(array('id'=>$this->user['id']))->save(array('email'=>$email));
                $this->success('綁定成功',U('Account/my'));
            }
        } else {
            $this->display();
        }
    }

小結:
1.這是一種思路,跟發送手機驗證碼差很少。
2.區別在於一個是發送短信,一個是發送郵件。
3.二一個,一個發送主體是阿里大魚,一個發送主體是公司申請的郵箱。
4.三一個,發送短信收費,發送郵件免費。函數

相關文章
相關標籤/搜索