So i'm devloping a small code that can control access for students / employees / visitors and strangers. So i'm facing this problem where the student when he Order his Unique QR code from a PHP form where the QR code is automaticly generated and encrypted and at the same time different from the ID of the student and at the end will be sent to the email of the student automaticly.
here is the code that i'm using rightnow :
<?php
include '../database/conn.php';
require_once '../assets/libraries/phpqrcode/qrlib.php';
require_once '../vendor/autoload.php';
if (isset($_POST['SubmitButton'])) {
function encrypt($data) {
$key = '2469'; // Replace this with your own secret key
$cipher = 'AES-256-CBC'; // Use a strong encryption cipher
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher)); // Generate a random IV
$encrypted = openssl_encrypt($data, $cipher, $key, OPENSSL_RAW_DATA, $iv); // Encrypt the data
$hmac = hash_hmac('sha256', $encrypted, $key, true); // Generate a HMAC for the encrypted data
return base64_encode($iv . $hmac . $encrypted); // Encode the IV, HMAC, and encrypted data as a base64 string
}
$user_id = $_POST['id'];
$email = $_POST['email'];
$fullname = $_POST['name'];
// Prepare the query
$query3 = ("SELECT * FROM ecrypted_ids WHERE id = '$user_id'");
// Bind the parameter
$result3 = mysqli_query($connection, $query3);
if (mysqli_num_rows($result3) > 0) {
echo '<br><div class="alert alert-info" role="alert">ID Already exist or wrong !</div>';
}else{
// Generate an encrypted ID related to the user's ID
$encrypted_id = encrypt($user_id); // You'll need to implement this function
// Save the encrypted ID in your database
// Prepare the query
$query4 = "INSERT INTO ecrypted_ids(id, encrypted_id) VALUES ('$user_id', '$encrypted_id')";
$result4 = mysqli_query($connection, $query3);
// Generate the QR code and save it to a file
QRcode::png($encrypted_id, 'qr_code.png');
sleep(3);
// Replace with the path to your service account's JSON key file
$keyFilePath = 'account.json';
// Replace with the email address that will be used to send the email
$senderEmail = 'bdeiramostephaamine@gmail.com';
// Replace with the recipient's email address
$recipientEmail = $_POST['email'];
// Replace with the subject of the email
$subject = 'Your QR code From UCBET';
// Replace with the body of the email
$body = 'Dear ' . $fullname . ',<br><br>' .
'Thank you for your order! Your QR code is attached to this email.<br><br>' .
'Best regards,<br>' .
'University Chadli Bendjedid Eltarf';
// Replace with the path to the QR code image file
$qrCodeImagePath = 'qr_code.png';
// Set up the Google API client
$client = new Google_Client();
$client->setAuthConfig($keyFilePath);
$client->addScope(Google_Service_Gmail::GMAIL_SEND);
$service = new Google_Service_Gmail($client);
// Set up the message
$message = new Google_Service_Gmail_Message();
$message->setRaw(base64_encode("To: $recipientEmail\r\n" .
"Subject: $subject\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"boundary\"\r\n\r\n" .
"--boundary\r\n" .
"Content-Type: text/html; charset=\"utf-8\"\r\n" .
"Content-Disposition: inline\r\n\r\n" .
"$body\r\n\r\n" .
"--boundary\r\n" .
"Content-Type: image/png\r\n" .
"Content-Disposition: attachment; filename=\"qrcode_". $fullname .".png\"\r\n\r\n" .
base64_encode(file_get_contents($qrCodeImagePath)) .
"\r\n\r\n" .
"--boundary--"));
// Send the email
try {
$message = $service->users_messages->send("me", $message);
echo '<br><div class="alert alert-success" role="alert">Your QR code has been sent to your email address!</div>';
} catch (Exception $e) {
echo '<br><div class="alert alert-danger" role="alert">An error occurred: ' . $e->getMessage() . '</div>';
}
}
}
?>
and at the end i always get this error
An error occurred: { "error": { "code": 400, "message": "Precondition check failed.", "errors": [ { "message": "Precondition check failed.", "domain": "global", "reason": "failedPrecondition" } ], "status": "FAILED_PRECONDITION" } }
I've already created a private account on the google cloud and allowed the owner permissions. Exported the json key from the service account and re-named to "private.json". but it doesn't work. Also im using the email associated to the app that've created in the google cloud platform as a sender email.
I've activated the service account to using the command
gcloud auth activate-service-account --key-file=path to the file
but nothing works !
I'll be grateful if you help me. Thanks.