PHP复习_PDO实战之实现注册邮件发送

项目准备

首先,我们要实现PHP的邮件发送,我们要准备一下邮件发送的插件,php的邮件插件现在市面上一共有三种常用,PhpMailerSwiftMailerZendMail

 

我们本次使用SwiftMailer,下面是本次项目实现代码分享

链接:https://pan.baidu.com/s/1tDSV_8hJdrReNa851Y5ZNQ

提取码:famt

SwiftMailer官方github地址:https://github.com/swiftmailer/swiftmailer.git

 

简要谈论SwiftMailer的使用

Swiftmailer是一个类似PHPMailer邮件发送组件,它也支持HTML格式、附件发送,但它发送效率相当高,成功率也非常高,很多PHP框架都集成了Swiftmailer

 

基本使用

只需填写邮箱服务器相关配置,然后填写邮件标题、发送对象和邮件内容,运行即可完成发送邮件任务:

require_once '/path/to/vendor/autoload.php';

$transport = (new Swift_SmtpTransport('smtp.163.com', 25)) // 邮箱服务器
->setUsername('your username')  // 邮箱用户名
->setPassword('your password')   // 邮箱密码,有的邮件服务器是授权码
;

$mailer = new Swift_Mailer($transport);

$message = (new Swift_Message('Wonderful Subject')) // 邮件标题
->setFrom(['john@doe.com' => 'John Doe']) // 发送者
->setTo(['receiver@domain.org', 'other@domain.org' => 'A name']) //发送对象,数组形式支持多个
->setBody('Here is the message itself') //邮件内容
;

$result = $mailer->send($message);

 

如果发送成功,会返回$result的值为1,即true

 

高级应用

发送邮件时最关键的是创建消息体,在Swift Mailer中创建消息是通过使用库提供的各种MIME实体完成的,因此我们不需要花太多时间去了解如何处理MIME实体,只需拿来使用即可。

Swift Mailer提供了创建邮件消息的一系列方法,下面列举我们常用到的一些方法:

setSubject():邮件主题

setFrom():发件人地址,数组形式,可以是多个发件人

setTo():收件人地址,数组形式,可以是多个收件人

setBody():邮件内容

addPart():邮件内容指定输出类型,支持html内容输出

attach():添加附件

setCc():抄送,支持多个邮箱地址

setBcc():密送,支持多个邮箱地址

 

项目实战

包含所需要文件

<?php
header('content-type:text/html;charset=utf-8');
//1.包含所需文件
require_once 'swiftmailer-master/lib/swift_required.php';
require_once 'PdoMySQL.class.php';
require_once 'config.php';
require_once 'pwd.ph

 

接收前端传递的信息

//2.接收信息
$act=$_GET['act'];
$username=addslashes($_POST['username']);//过滤用户输入内容
$password=md5($_POST['password']);
$email=$_POST['email'];
$table='user';

 

得到PDO连接对象并完成注册

//3.得到连接对象
$PdoMySQL=new PdoMySQL();
if($act==='reg'){
   $regtime=time();
   //完成注册的功能
   $token=md5($username.$password.$regtime);
   $token_exptime=$regtime+24*3600;//过期时间
   $data=compact('username','password','email','token','token_exptime','regtime');
   $res=$PdoMySQL->add($data, $table);
   $lastInsertId=$PdoMySQL->getLastInsertId();

 

准备发送激活邮件信息

    if($res){
      //发送邮件,以QQ邮箱为例
      //配置邮件服务器,得到传输对象
      $transport=Swift_SmtpTransport::newInstance('smtp.qq.com',25);
      //设置登陆帐号和密码
      $transport->setUsername('490222113@qq.com');
      $transport->setPassword($emailPassword);
      //得到发送邮件对象Swift_Mailer对象
      $mailer=Swift_Mailer::newInstance($transport);
      //得到邮件信息对象
      $message=Swift_Message::newInstance();
      //设置管理员的信息
      $message->setFrom(array('490222113@qq.com'=>'King'));
      //将邮件发给谁
      $message->setTo(array($email=>'imooc'));
      //设置邮件主题
      $message->setSubject('激活邮件');
      $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?act=active&token={$token}";
      $urlencode=urlencode($url);
      $str=<<<EOF
      亲爱的{$username}您好~!感谢您注册我们网站<br/>
      请点击此链接激活帐号即可登陆!<br/>
      <a href="{$url}">{$urlencode}</a>
      <br/>
      如果点此链接无反映,可以将其复制到浏览器中来执行,链接的有效时间为24小时。    
EOF;
      $message->setBody("{$str}",'text/html','utf-8');

 

发送邮件

try{
   if($mailer->send($message)){
      echo "恭喜您{$username}注册成功,请到邮箱激活之后登陆<br/>";
      echo '3秒钟后跳转到登陆页面';
      echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
   }else{
      $PdoMySQL->delete($table,'id='.$lastInsertId);
      echo '注册失败,请重新注册';
      echo '3秒钟后跳转到注册页面';
      echo '<meta http-equiv="refresh" content="3;url=index.php#toregister"/>';
   }
}catch(Swift_ConnectionException $e){
   echo '邮件发送错误'.$e->getMessage();
}

 

完成激活操作

}elseif($act==='active'){
   $token=addslashes($_GET['token']);
   $row=$PdoMySQL->find($table,"token='{$token}' AND status=0",array('id','token_exptime'));
   $now=time();
   if($now>$row['token_exptime']){
      echo '激活时间过期,请重新登陆激活';
   }else{
      $res=$PdoMySQL->update(array('status'=>1),$table,'id='.$row['id']);
      if($res){
         echo '激活成功,3秒钟后跳转到登陆页面';
         echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
      }else{
         echo '激活失败,请重新激活';
         echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
      }
   }

 

完成登录操作

}elseif($act==='login'){
   //完成登陆的功能
   $row=$PdoMySQL->find($table,"username='{$username}' AND password='{$password}'",'status');
   if($row['status']==0){
      echo '请先激活在登陆';
      echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
   }else{
      echo '登陆成功,3秒钟后跳转到首页';
      echo '<meta http-equiv="refresh" content="3;url=http://www.imooc.com"/>';
   }

 

 

注意,你的发件邮箱请务必开启该服务,否则可能发送失败

QQ邮箱为例,路径在邮箱设置->账号下

PHP复习_PDO实战之实现注册邮件发送4558.png

 

博客
32132
07-14 365
07-12 298
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值