phpmailer 邮件模拟注册验正

下载phpmailer类 我本次的实验用的是版本 5.2.9

下载后解压提取文件class.smtp.php

class.phpmailer.php

PHPMailerAutoload.php

放在phpmailer目录里

1.链接数据库

conn.php 

 $conn=mysql_connect("localhost","root","");
   if(!$conn){
  die('链接失败'.mysql_error());
   }
   
   mysql_select_db("test",$conn) or die("数据库选择失败".mysql_error());


2创建数据库表

create table user(
uid int primary key auto_increment,
uname char(20) not null default '',
email char(30) not null default '',
pass char(32) not null default '',
status tinyint not null default 0
)engine myisam charset utf8;


create table activecode(
cid int primary key auto_increment,
uname char(20) not null default '',
code char(16) not null default '',
expire int not null default 0
)engine myisam charset utf8;


3.模拟注册和发送邮件代码如下

<?php 
   /**
     1.链接数据库
2.insert用户表
3.随机产生激活码并insert activecode 表
4把产生的激活码发送邮件
 
   */
require('conn.php');
require('../phpmailer/PHPMailerAutoload.php');
$uname="username";
$email="99999999999999@qq.com";


//2注册
$sql="insert into user(uname,email) values('$uname','$email')";
mysql_query($sql);


//3生成随机激活码
$str="abcdefghijklmnopkrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
$code=substr(str_shuffle(substr($str,0,58)),0,8);
$expire=time()+2*24*3600;
$sql="insert into activecode(uname,code,expire)values('$uname','$code',$expire)";
$result=mysql_query($sql);


if(!$result){

die ("注册不成功");
}


//4发送邮件进行验证
function sendEmail($to,$title,$contents,$type = '',$accessory =''){
    $mail = new PhpMailer(true);
    $mail->IsSMTP();
    $mail->CharSet ="UTF-8";//编码
    $mail->Debugoutput = 'html';// 支持HTML格式
    $mail->Host = 'smtp.163.com';//HOST 地址
    $mail->Port = 25;//端口
    $mail->SMTPAuth = true;
    $mail->Username = 'php100@163.com';//163邮箱用户名
    $mail->Password = '163邮箱密码你的密码';//密码
    $mail->SetFrom('php100@163.com','fuck');//发件人地址, 发件人名称
    $mail->AddAddress($to);//收信人地址
    //$mail->Subject = "=?utf-8?B?" . base64_encode() . "?=";
    if (!empty($type)) {
     $mail->AddAttachment($type,$accessory); // 添加附件,并指定名称 
    }
    $mail->Subject = $title;//邮件标题
    $mail->MsgHTML($contents);
    if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    }else {
    echo "Message sent!";
}
    }
  sendEmail($email,$uname.'验证邮件','请您点击:http://localhost/test2/loginmail/2.php?code='.$code.' 进行激活','','');


?>

4.验证激活码 完成注册 2.php代码如下

require "conn.php";
   $code=$_GET['code'];
   if(strlen($code)!=8){
  
  die("激活码长度不够");
  
   }
   $sql="select * from activecode where code='$code'";
   $rs=mysql_query($sql);
   $row=mysql_fetch_assoc($rs);
   if(empty($row)){
  echo "激活码有误";
  
   }
   var_dump($row);
   
   //激活用户
    $sql="update user set status=1 where uname='$row[uname]'";
    $rs2=mysql_query($sql);
if($rs2){
echo "激活成功";

}


以上是我这个菜鸟的总结 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值