由於專案中需要做一個郵箱驗證。一直在網上找各種資料,出現各種傳送不了郵箱。最後在我堅持下終於可以在使用qq傳送驗證郵箱了。想了解更多相關資訊,請持續關注我們應屆畢業生考試網!
1,先去qq郵箱中的 點選設定-》點選帳戶-》將smtp的許可權開啟
如果是設定POP3和SMTP的加密方式,則埠如下:
POP3服務器(埠995)
SMTP伺服器(埠465或587)。
2,檢視你的Openssl和Socketsd是否支援:PHP -m檢視
這裡寫圖片描述
利用OpenSSL庫對Socket傳輸進行安全加密。
3,表單部分
<form id="reg" action="" method="post">
<p>使用者名稱:<input type="text" class="input" name="username" id="user"></p>
<p>密 碼:<input type="password" class="input" name="password" id="pass"></p>
<p>E-mail:<input type="text" class="input" name="email" id="email"></p>
<p><input type="submit" class="btn" value="提交註冊"></p>
</form>
4,表單提交部分
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>PHP使用者註冊郵箱驗證啟用帳號</title>
<style type="text/css">
{margin: 20px auto; width: 400px; border: 1px solid #ccc; line-height: 50px;text-align: center;}
t {width: 150px; height: 25px; border: 1px solid #ccc;}
{padding: 5px 15px; font-size: 16px; font-family: '微軟雅黑'; background:#ff0066; color: #fff; border: none;}
</style>
<script type="text/javascript">
function chk_form(){
var user = lementById("user");
if(e==""){
alert("使用者名稱不能為空!");
return false;
//s();
}
var pass = lementById("pass");
if(e==""){
alert("密碼不能為空!");
return false;
//s();
}
var email = lementById("email");
if(e==""){
alert("Email不能為空!");
return false;
//s();
}
var preg = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/; //匹配Email
if(!(e)){
alert("Email格式錯誤!");
return false;
//s();
}
}
</script>
</head>
<body>
<p id="main">
<p class="demo">
<form id="reg" action="" method="post" onsubmit="return chk_form();">
使用者名稱:<input type="text" class="input" name="username" id="user"><br>
密 碼:<input type="password" class="input" name="password" id="pass"><br>
郵 箱:<input type="text" class="input" name="email" id="email"><br>
<input type="submit" class="btn" value="提交註冊">
</form>
</p>
</p>
</body>
</html>
5,如果註冊成功,傳送郵箱驗證碼,這裡負責傳送。
部分
<?php
include_once("");//連線資料庫
// include_once("");//郵件傳送類
$username = stripslashes(trim($_POST['username']));
$sql ="select id from t_user where username='{$username}'";
// $query = mysqli_query("'");
$res = mysqli_query($link, $sql);
if($res && mysqli_num_rows($res)>0){
echo '使用者名稱已存在,請換個其他的使用者名稱';
exit;
}
//構造啟用碼
$password = md5(trim($_POST['password'])); //加密密碼
$email = trim($_POST['email']); //郵箱
$regtime = time();
$token = md5($username.$password.$regtime); //建立用於啟用識別碼
$token_exptime = time()+60*60*24;//過期時間為24小時後
$sql = "insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`)
values ('$username','$password','$email','$token','$token_exptime','$regtime')";
// echo $sql;
$res1 = mysqli_query($link, $sql);
if(mysqli_insert_id($link)){
//郵件傳送
require '';
require '';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '; // SMTP username
$mail->Password = 'dzrxckopdnxuhjhf'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom(', '發件人');
$email = $_POST['email'];
$mail->addAddress($email, '.'); // Add a recipient
// Name is optional
$mail->addReplyTo($email, 'php');
//$mail->addCC(');
//$mail->addBCC(');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = '標題';
$mail->Body = "傳送的內容";
// $mail->AltBody = '傳送的內容22';
if(!$mail->send()) {
//輸出錯誤資訊
echo 'Mailer Error: ' . $mail->ErrorInfo;
return false;
} else {
echo 'Message has been sent'; //成功輸出
return true;
}
}
?>
6 ,驗證部分
include_once("");//連線資料庫
$verify = stripslashes(trim($_GET['verify']));
$nowtime = time();
$sql = "select id,token_exptime from t_user where status='0' and `token`='$verify'";
$res= mysql_query($link, $sql);
$row = mysql_fetch_array($res);
if($row){
if($nowtime>$row['token_exptime']){ //24hour
$msg = '您的啟用有效期已過,請登入您的帳號重新發送啟用郵件.';
}else{
$sql1 ="update t_user set status=1 where id=".$row['id']";
$res1 = mysqli_query($link, $sql1);
if(mysqli_affected_rows($link)!=1) die(0);
$msg = '啟用成功!';
}
}else{
$msg = 'error.';
}
echo $msg;