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();
}
}

小结:

1.这是一种思路,跟发送手机验证码差不多。

2.区别在于一个是发送短信,一个是发送邮件。

3.二一个,一个发送主体是阿里大鱼,一个发送主体是公司申请的邮箱。

4.三一个,发送短信收费,发送邮件免费。

phpmailer绑定邮箱的更多相关文章

  1. AspNetCore-MVC实战系列(二)之通过绑定邮箱找回密码

    AspNetCore - MVC实战系列目录 . 爱留图网站诞生 . AspNetCore - MVC实战系列(一)之Sqlserver表映射实体模型 . AspNetCore-MVC实战系列(二)之 ...

  2. PHPMailer发送邮箱(ThinkPHP实战篇)

    1.下载phpmailer文件库 2.引用文件,此处将代码放到 :函数库中,function.php function sendConsultantMessage($sendData){ Vendor ...

  3. PHPMailer发送邮箱

    1.可以参考的链接.http://www.helloweba.com/view-blog-205.html 2.下载最新的PHPMailer文件库 3.主要代码 class.phpmailer.php ...

  4. PHP(ThinkPHP5.0) + PHPMailer 进行邮箱发送验证码

    GitHub下载最新版第三方类库PHPMailer: 第一步: 打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 需要 ...

  5. JIRA绑定邮箱

    [管理-系统-外发邮件] 之前配置QQ邮箱没有成功,后来使用阿里邮箱一次就成功了 [保存]成功后可以测试一下

  6. Java案例-用户注册邮箱绑定激活功能实现

    <–start–> 需求描述:当客户打开收到邮箱激活码的邮件,点击激活链接,正确填写激活码后就会完成邮箱激活的步骤. 在后台编程代码编写中,有以下几个要点: ① 接收客户的手机号码和邮箱激 ...

  7. outlook2016用Exchange轻松绑定腾讯企业邮箱

    系统版本:Win10 X64 1709 英文版 邮箱:Outlook2016 背景知识: 1.发送邮件均使用SMTP协议(SMTP 全称“Simple Mail Transfer Protocol”, ...

  8. Django商城项目笔记No.14用户部分-用户中心邮箱绑定

    保存邮箱界面如下 接口设计如下 视图逻辑: 因为url是不接受pk参数的,所以UpdateApiView无法确定要更新哪个模型类,所以要重写get_object,告诉他更新哪个模型类.这里更新的是us ...

  9. thinkphp phpmailer邮箱验证

    thinkphp 关于phpmailer的邮箱验证 一  . 登陆自己的邮箱,例如:qq邮箱.登陆qq邮箱在账户设置中开启smtp服务: 之后回发送一个授权码 , 这个授权码先保存下来,这个授权码在后 ...

随机推荐

  1. AtCoder ARC097C Sorted and Sorted:dp

    传送门 题意 有 $ 2n $ 个球排成一行,其中恰好有 $ n $ 个白球和 $ n $ 个黑球.每个球上写着数字,其中白球上的数字的并集为 $ \lbrace 1 \dots n\rbrace $ ...

  2. linux中的kill命令

    一. 定义 kill命令用来删除执行中的程序或工作.kill可将指定的信息送至程序.预设的信息为SIGTERM(15),可将指定程序终止.若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除 ...

  3. Mybatis输入和输出映射(parameterType和resultType的区别)

    parameterType                                                                             resultType ...

  4. Linux 下硬链接和软链接的说明

    Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln 命令产生硬链接. 硬连接 硬连接指通过索引节点来进行连接.在 Li ...

  5. ZOJ 3488 Conic Section

    The conic sections are the nondegenerate curves generated by the intersections of a plane with one o ...

  6. Agilent RF fundamentals (10) Mixer ,Phase domain and modulator

    1 Mixer characterization DC input Bias voltage Bias Current RF input Lo input IF output 2 mixer devi ...

  7. DRF 返回数据的封装,和分页

    DRF 返回数据的封装,和分页 1 返回值的 封装 自定义一个类,初始化基本的返回数据信息 class BaseResponse(object): """ 初始化基本的返 ...

  8. Helix Server 支持的文件格式

    比如,对于WMV格式的文件,访问路径可以是:mms://192.168.1.1/mov/music.wmv 对于rm格式的文件rtsp://192.168.1.1/mov/123.rm 但,比如对于格 ...

  9. php果然是世界上最好的语言

    这两天参加Hackathon,作为一个什么都半吊子的家伙,两人小队伍被逼上岗,于是我不得不着手写代码.由此,我体验到了php的魔力-- 首先,我深刻地意识到了更新版本的重要性. 偷懒不想搭Apache ...

  10. SVN客户端与服务器端搭建操作

    一.客户端的安装 1.点击安装程序 2.修改svn安装位置 3.开始安装 4.客户端安装成功 5.回到左面  右键出现svn检出 tortoiSVN  表示安装成功 Myeclipse svn插件安装 ...