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' => '點(diǎn)多多',
'email_subject' => '助店寶商戶激活郵箱',
'email_body' => "尊敬的用戶{$username}您好:
您的激活碼為<font color='red'>{$code}</font>,請(qǐng)將激活碼輸入進(jìn)行驗(yàn)證! 激活碼有效期為6分鐘^_^",
);
2.發(fā)送函數(shù)
// 發(fā)送郵件
private function _sendEmail($email,$code,$username = '') {
import('@.ORG.phpmailer');
$mail = new PHPMailer(); //建立郵件發(fā)送類,類名不一定與引入的文件名相同
$mail->CharSet = "UTF-8";
$mail->IsSMTP(); // 使用SMTP方式發(fā)送
$mail->Host = C('email_host'); // 您的企業(yè)郵局域名
$mail->SMTPAuth = true; // 啟用SMTP驗(yàn)證功能
$mail->Username = C('email_username'); // 郵局用戶名(請(qǐng)?zhí)顚懲暾膃mail地址)
$mail->Password = C('email_password'); // 郵局密碼
$mail->Port=C('email_port');
$mail->From = C('email_from'); //郵件發(fā)送者email地址
$mail->FromName = C('email_fromname');
$mail->AddAddress("$email", "$username");
$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
$mail->Subject = C('email_subject'); //郵件標(biāo)題
$email_body = "尊敬的用戶<strong>{$username}</strong>您好:
您的激活碼為<font color='red'>{$code}</font>,請(qǐng)將激活碼輸入進(jìn)行驗(yàn)證! 激活碼有效期為6分鐘^_^";
$mail->Body = $email_body; //郵件內(nèi)容衡蚂,上面設(shè)置HTML窿克,則可以是HTML
if(!$mail->Send())
{
return array('status'=>2,'info'=>$mail->ErrorInfo);
} else {
return array('status'=>1,'info'=>'發(fā)送成功');;
}
}
3.生成驗(yàn)證碼保存到session中骏庸,并發(fā)送
// 發(fā)送郵箱激活碼
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) {
//設(shè)置發(fā)送限制時(shí)間
session($this->activationtime, time() + 50);
session($this->activationcode, array('code' => $activationcode, 'time' => time() + 600));
$this->ajaxReturn(array('result' => true));
} else {
//發(fā)送失敗寫入日志文件
$log = date('Y-m-d H:i:s') . " 發(fā)送失敗:{$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' => '錯(cuò)誤的請(qǐng)求'));
}
}
4.驗(yàn)證并綁定
// 綁定郵箱
public function bind_email() {
if (IS_POST) {
// 獲取驗(yàn)證碼
$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('驗(yàn)證碼有誤');
} else {
M('User')->where(array('id'=>$this->user['id']))->save(array('email'=>$email));
$this->success('綁定成功',U('Account/my'));
}
} else {
$this->display();
}
}
小結(jié):
1.這是一種思路毛甲,跟發(fā)送手機(jī)驗(yàn)證碼差不多。
2.區(qū)別在于一個(gè)是發(fā)送短信具被,一個(gè)是發(fā)送郵件玻募。
3.二一個(gè),一個(gè)發(fā)送主體是阿里大魚(yú)一姿,一個(gè)發(fā)送主體是公司申請(qǐng)的郵箱七咧。
4.三一個(gè),發(fā)送短信收費(fèi)叮叹,發(fā)送郵件免費(fèi)艾栋。