Skip to content

Instantly share code, notes, and snippets.

@Nikoloutsos
Created April 1, 2020 15:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Nikoloutsos/b9e785ef0720cee225c7a0dcd828edc9 to your computer and use it in GitHub Desktop.
Sharing code with John pragias
<?php
// Get student email
$email = isset($_POST['email']) ? $_POST['email'] : die();
// Include third party library classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../util/phpmailer/Exception.php';
require '../util/phpmailer/PHPMailer.php';
require '../util/phpmailer/SMTP.php';
include_once '../util/objects/config/Database.php';
// Generate PIN
$pin = generatePIN(5);
// Send email
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = "unipoll.info@gmail.com";
$mail->Password = "xxxxxx";
$mail->setFrom("unipoll.info@gmail.com", "Unipoll");
$mail->addAddress($email);
$mail->Subject = 'UniPoll verification PIN'; //todo Write a nice sentence
$mail->msgHTML("Authentication PIN: ". $pin);
$mail->AltBody = "Authentication PIN: ". $pin;
if(!$mail->send()){
echo json_encode(["status" => "0", "error_msg" => $mail->ErrorInfo]);
}else{
echo json_encode(["status" => "1", "pin" => $pin]);
storePinToDatabase($email, $pin);
}
// Generates a PIN
function generatePIN($digits = 4){
$i = 0;
$pin = "";
while($i < $digits){
$pin .= mt_rand(0, 9);
$i++;
}
return $pin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment