kirti003shukla

Sending account activation link

Apr 16th, 2020
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.41 KB | None | 0 0
  1.     <?php
  2.     require "phpMailer/PHPMailer/PHPMailerAutoload.php";
  3.             $mail = new PHPMailer();    
  4.     // initializing variables
  5.     $errors = array();
  6.    
  7.     // connect to the database
  8.     $db = mysqli_connect('localhost', 'servername', 'password', 'gac');
  9.    
  10.     // REGISTER USER
  11.     if (isset($_POST['reg_user'])) {
  12.       // receive all input values from the form
  13.       $username = mysqli_real_escape_string($db, $_POST['username']);
  14.       $email = mysqli_real_escape_string($db, $_POST['email']);
  15.       $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  16.       $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  17.       $_SESSION['username'] = $username;
  18.       $_SESSION['success'] = "You are now successfully registered";
  19.      
  20.       // form validation: ensure that the form is correctly filled ...
  21.       // by adding (array_push()) corresponding error unto $errors array
  22.       if (empty($username)) { array_push($errors, "Username is required"); }
  23.       if (empty($email)) { array_push($errors, "Email is required"); }
  24.       if (empty($password_1) || strlen($password_1)<8) { array_push($errors, "Password is required and must be 8 length in characters"); }
  25.    
  26.       if ($password_1 !== $password_2) {
  27.         array_push($errors, "The two passwords do not match");
  28.       }
  29.    
  30.       // first check the database to make sure
  31.       // a user does not already exist with the same username  email
  32.       $user_check_query = "SELECT * FROM registered_users WHERE email='$email' LIMIT 1";
  33.       $result = mysqli_query($db, $user_check_query);
  34.       $user = mysqli_fetch_assoc($result);
  35.      
  36.       if ($user) { // if user exists
  37.        /* if ($user['username'] === $username) {
  38.           array_push($errors, "Username already exists");
  39.         }*/
  40.    
  41.         if ($user['email'] === $email) {
  42.           array_push($errors, "email already exists");
  43.         }
  44.       }
  45.    
  46.       // Finally, register user if there are no errors in the form
  47.       if (count($errors) == 0) {
  48.          
  49.           $v_key = md5($email.time());//generate the vkey for database
  50.          $v_addKey = substr(md5(uniqid(rand(),1)),3,8);
  51.             $v_key = $v_addKey.$v_key;
  52.            
  53.         $password = md5($password_1);//encrypt the password before saving in the database
  54.        
  55.         //making expiry link for activation of account
  56.          $exp_acc_act_Format = mktime(
  57.        date("H")+1, date("i"), date("s"), date("m") ,date("d"), date("Y")
  58.        );
  59.        $expHour = date("Y-m-d H:i:s",$exp_acc_act_Format);
  60.      
  61.    
  62.         $query = "INSERT INTO registered_users (username, email, password, status, v_key, expHour)  VALUES('$username', '$email', '$password',0,'$v_key','$expHour')";
  63.        
  64.         $insert = mysqli_query($db, $query);
  65.         if($insert)// after insert send verification email
  66.         {
  67.             $output='<p>Dear user,</p>';
  68.     $output.='
  69.    <p>Please click on the following button to activate your account.</p>';
  70.    
  71.     $output.='<center style ="border:1px solid black;height:40px;width:200px;background-color:#005b2d;">
  72.    <p>
  73.    <a href="http://www.sanjeevsanthalia.com/thankyou.php?v_key='.$v_key.'&email='.$email.'&action=reset" target="_blank" style ="color:white;text-decoration:none;">
  74.    <strong>
  75.    Click here to verify the email
  76.    </strong>
  77.    </a>
  78.    </p>
  79.     </center>';    
  80.     $output.='<p>Thanks,</p>';
  81.     $body = $output;
  82.     $subject = "Account Activation -GACKONNECT ";
  83.     $email_to = $email;
  84.     $fromserver = "noreply@gacons.com";
  85.     //$mail->IsSMTP();/*by commenting also it was working*/
  86.     $mail->SMTPAuth = true;
  87.     $mail->SMTPSecure = 'ssl';
  88.     $mail->Host = "hello.com"; // Enter your host here
  89.     $mail->Port = 465;
  90.     $mail->Username = "noreply@hello.com"; // Enter your email here
  91.     $mail->Password = "password"; //Enter your password here
  92.     $mail->IsHTML(true);
  93.     $mail->From = "noreply@hello.com";
  94.     $mail->FromName = "GACKONNECT";
  95.     $mail->Sender = $fromserver; // indicates ReturnPath header
  96.     $mail->Subject = $subject;
  97.     $mail->Body = $body;
  98.     $mail->AddAddress($email_to);
  99.    
  100.     if(!$mail->send()){
  101.     echo "Mailer Error: " . $mail->ErrorInfo;
  102.     }else{
  103.         header('location:afterRegistration.php');
  104.    
  105.         }
  106.          
  107.         }
  108.       }
  109.     }
  110.  
  111. It was working but suddenly stopped. I am not able to figure out. Receiving data in database also only mail is not sending.
Add Comment
Please, Sign In to add comment