今天就跟大家聊聊有關如何使用phpmailer對郵箱進行綁定,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1.配置
<?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.發送函數
// 發送郵件
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();
}
}看完上述內容,你們對如何使用phpmailer對郵箱進行綁定有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。