本文实例讲述了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程序设计有所帮助。