phpmailer綁定郵箱的實現方法

來源:互聯網
上載者:User
本文執行個體講述了phpmailer綁定郵箱的實現方法。分享給大家供大家參考,具體如下:

效果如下:

1.配置

<?phpreturn 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.發送函數

// 發送郵件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中,並發送

// 發送郵箱啟用碼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. 三一個,傳送簡訊收費,發送郵件免費。

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.