phpmail发送邮件

 首先来输入收件人用户名与验证邮箱

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	   body {
            margin: 500px 0;
            text-align: center;			
        }
	</style>
</head>
<body>
	<form action="1.php" method="post">
		<input type="text" placeholder="用户名" name="uname">
		<input type="text" placeholder="验证邮箱" name="email">
		<input type="submit" value="提交">
	</form>	
</body>
</html>

下面模拟注册用户+创建激活码+发送激活码
思路:
1:链接数据库(代码省略)
2:insert用户表
3:随机生成激活码并insert activecode表
4:将生成的激活码发送大注册邮箱

// 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;
header("Content-type: text/html; charset=utf-8");
// 加载需要文件
require('./conn.php');
require('./PHPMailer/class.phpmailer.php');
$uname=$_POST['uname'];
$email=$_POST['email'];

// 第二步模拟注册
$sql="insert into user(uname,email) value('$uname','$email')";
mysql_query($sql,$conn);

// 第三步 生成激活码
$str='abcABC';
$code=substr(str_shuffle(substr($str, 0,52)), 0,6);//生成随机用户名
$expire=time()+5*24*3600;//设置验证码有效期
$sql2="insert into activecode(uname,code,expire) value('$uname','$code','$expire')";
mysql_query($sql2,$conn);

// 第四步 发送激活邮件
$phpmail=new phpmailer();
$phpmail->IsSMTP();
$phpmail->Host='smtp.qq.com';//可以用smtp.qq.com或者smtp.163.com做测试
$phpmail->Charset='utf8';
$phpmail->SMTPAuth=true;
$phpmail->SMTPSecure = "25";// 加密方式:不加密(端口25);SSL加密(端口465);TLS加密(端口587)默认不填也可以
$phpmail->Port = '';// 默认不填也可以
$phpmail->Username='***@qq.com';//发信方qq邮箱
$phpmail->Password='***';//发送方的邮箱密码,注意用QQ邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!

// 可以发信了
$phpmail->From='***@qq.com';//发信人邮箱地址
$phpmail->FromName='小明';//发信人姓名
$phpmail->Subject=$uname.'欢迎您注册,你的激活邮件';//发件主题
$phpmail->Body='点击链接激活:http://localhost/test/2.php?code='.$code;//内容

// 添加收件人
$phpmail->ADDADDress('***@qq.com','伟大的小明');//设置收件人信息

// 发信
echo $phpmail->send()?'<script>confirm("邮件已发送请注意查收");history.go(-1);</script>':'<script>confirm("邮件发送失败");history.go(-1);</script>';

下面这个页面用来激活用户
步骤:
1:从地址栏获取激活码
2:查询激活码
    2.1 激活码不存在,报错
    2.2 激活码存在,但过期,提示
    2.3 激活码正常,就激活对应的用户(update user --> status->1)
    2.4 把激活码expire改为-1

<?php 
header("Content-type: text/html; charset=utf-8");
require('./conn.php');
$code=$_GET['code'];
if(strlen($code)!=6){
	exit('激活码错误');
}
$sql="select * from activecode where code = '$code'";
$rs=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($rs);//获取结果
// 判断激活码是否存在
if(empty($row)){
	exit('激活码不对');
}
// 判断激活码是否过期
if(time()>$row['expire']){
	exit('激活码已过期,请重新激活');
}
// 激活用户
$sql1="update user set status=1 where uname='$row[uname]'";
mysql_query($sql1,$conn);

// 把此次激活作废
$sql2="update activecode set expire=0 where code='$code'";
mysql_query($sql2,$conn);
echo '<script>confirm("你已经成功验证");window.location.href="1.html"</script>';

实现HTTPS协议访问,就需要SSL证书,TLS是SSL与HTTPS安全传输层协议名称。

QQ邮箱如何打开POP3/SMTP/IMAP功能?service.mail.qq.com/cgi-bin/help?subtype=1&&no=166&&id=28

邮件服务端口 https://blog.csdn.net/zhangpan19910604/article/details/45065629
SSL与TLS的区别以及介绍:https://www.gworg.com/ssl/422.html
HTTPS加密协议详解(四):TLS/SSL握手过程:https://www.gworg.com/ssl/408.html
HTTPS和HTTP的区别是什么?:https://www.gworg.com/ssl/404.html

原文地址:https://www.cnblogs.com/aten/p/9239044.html