Saturday, 5 March 2022

PHP#32MySQL#28 - Ajax3 + EMAIL

//connect.php

<?php
$link=@mysqli_connect("localhost","root","") or die("Khong the ket noi den server");
mysqli_select_db($link,"webtintuc") or die ("Kiem tra lai ten database!");
mysqli_query($link,"set names 'utf8'");
?>

//dangky.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="xuly_dangky.php">
  <p>
    <label for="hoten">Họ tên:</label>
    <input type="text" name="hoten" id="hoten">
  </p>
  <p>
    <label for="diachi">Dia chi:</label>
    <input type="text" name="diachi" id="diachi">
  </p>
  <p>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username">
  </p>
  <p>
    <label for="pass">Password:</label>
    <input type="text" name="pass" id="pass">
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email">
  </p>
  <p>
    <input type="submit" name="dangky" id="dangky" value="Đăng ký">
  </p>
</form>
</body>
</html>

//xuly_dangky.php

<?php
if(isset($_POST['dangky']))
{
include("connect.php");
$ngaydk=date("Y-m-d",time());
$random=md5(rand(99,999));
$sl="insert into webtm_user values(NULL, '{$_POST['hoten']}', '{$_POST['diachi']}', '0123456789', '{$_POST['username']}', '{$_POST['pass']}', '{$_POST['email']}', '$ngaydk', 0, NULL, NULL, 0, '$random', 0)";
if(mysqli_query($link,$sl))
{
echo "Đăng ký thành công!";
include("gmail.php");
}
else echo $sl;
}
?>

//active.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_GET['key']))
{
include("connect.php");
$key=$_GET['key'];
$sl="update webtm_user set Active=1, RandomKey='' where RandomKey='$key'";
if(mysqli_query($link,$sl))
echo "Tài khoản đã được kích hoạt thành công!.......";
else echo $sl;
}
?>
</body>
</html>

//gmail.php

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'PHPMailer-master/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';

$mail->Port = 465; //465

$mail->SMTPSecure = 'ssl'; //ssl

$mail->SMTPAuth = true;

$mail->Username = "";

$mail->Password = "";

$mail->setFrom('', 'Nguyen Van Teo');

$mail->addReplyTo('', 'Nguyen Van Teo');

$mail->addAddress($_POST['email'], $_POST['hoten']);

$mail->Subject = 'Kích hoạt tài khoản!';

$mail->msgHTML("Xin chào bạn! Click vào <a href='http://localhost:8080/tintuc/dangky/active.php?key=$random'>đây</a> để kích hoạt tài khoản");
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

0 comments:

Post a Comment