本文实例讲述了phpmailer绑定邮箱的实现方法。分享给大家供大家参考,具体如下:
效果如下:
1.配置
?
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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.发送函数
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// 发送邮件
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中,并发送
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 发送邮箱激活码
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.验证并绑定
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 绑定邮箱
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程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
linux系统中通过rsync+inotify实现网页自动同步
2025-05-27 45 -
2025-05-27 18
-
2025-06-05 55
-
spring Mvc配置xml使ResponseBody返回Json的方法示例
2025-05-29 93 -
asp.net中如何批量导出access某表内容到word文档
2025-05-29 102
热门评论