糯米文學吧

位置:首頁 > 計算機 > php語言

PHP郵件發送驗證

php語言2.31W

由於項目中需要做一個郵箱驗證。一直在網上找各種資料,出現各種發送不了郵箱。最後在我堅持下終於可以在使用qq發送驗證郵箱了。想了解更多相關信息,請持續關注我們應屆畢業生考試網!

PHP郵件發送驗證

  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>

密&nbsp;碼:<input type="password" class="input" name="password" id="pass"><br>

郵&nbsp;箱:<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;

標籤:PHP 郵件 驗證 發送