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. Java中使用Log的方法

    一.java自带log:java.util.logging.Logger使用三步曲 public class HelloLogWorld { private static String name = ...

  2. 最详细的虚拟机安装centos7教程

    参考网站:https://jingyan.baidu.com/article/b7001fe1d1d8380e7382dd72.html 安装vmware10,vmware是收费的,作为学习,我介意还 ...

  3. require.js资料

    1.http://www.ruanyifeng.com/blog/2012/11/require_js.html?bsh_bid=230697246 (require.js的用法) 2.http:// ...

  4. python面向对象编程 继承 组合 接口和抽象类

    1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均用点来访问自己的 ...

  5. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  6. LeetCode OJ:Roman to Integer(转换罗马字符到整数)

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  7. 感觉有变良好的第一次电面——yahoo北京测试实习生

    一个月之前投的岗位.都已经忘了.昨天突然接到电话说今天下午3点电面. 立马又开始忐忑起来,整理了下项目啊,推荐系统相关知识啥的,跑到欧巴桑寝室去电面电面. 3点很准时的电话来了,是个女面试官. 一上来 ...

  8. PS基础教程[3]如何去除照片上的水印

    网络上的照片大部分都有很多的水印,要嘛就是网站的地址,要嘛就是一些煽情的文字,我们看图片想要的可不是这些东西,那么我们怎样去掉图片上的水印呢?本次我们就来分享一下仿制图章工具的使用. 方法 1.打开P ...

  9. matlab滤波器的设计

    求出濾波器的階數以及 3dB 截止頻率後,可用相應的 Matlab 函數計算出實現傳遞函數的分子分母係數來.巴特沃斯型濾波器是通帶內最大平坦.帶外單調下降型的,其計算命令是:[b,a] = butte ...

  10. return super(ParamValueInline,self).formfield_for_foreignkey(db_field,request,**kwargs)自己返回自己的父类

    作者:刘强胜链接:https://www.zhihu.com/question/30361435/answer/83940591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...