I have encoutenered a road block with my code. I was following some tutorials, I managed to separatly create an qr code generator with QRCode.js (it takes PersonID from the server, a forwards it as data for the generator). However, as I intend to send the generated qr code to the receiver through PHPMailer library, I don’t know how to transfer the created qr code image to the PHPMailer’s body or addAttachment. Perhaps someone has any ideas and can help me with that?
This is code that is getting the PersonID from the server
$select = "SELECT PersonID FROM Users WHERE FirstName='$FirstName' AND LastName='$LastName' AND Email='$Email'";
$query = mysqli_query($db, $select);
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_assoc($query);
$personID = $row['PersonID'];
}
This is JS code qr generator
<script>
var personID = <?php echo $personID; ?>;
var QRImage = document.getElementById("QRImage");
var NewImage = new QRCode(QRImage, {
width: 200,
height: 200
});
function generateAndSubmit() {
var data = personID.value;
// Generate the QR code
NewImage.makeCode(data);
// Store the QR code value in the hidden input field
var qrCodeValueInput = document.getElementById("qrCodeValue");
qrCodeValueInput.value = QRImage.toDataURL();
}
</script>
And this is PHPMailer email form
$mail->Subject = "Registration confrirmation";
$mail->Body = '<p>Thank you for registering!</p>
<li>First Name: '.$FirstName.'</li>
<li>Last Name: '.$LastName.'</li>
<li>Email: '.$Email.'</li>
<li>Age: '.$Age.'</li>
<li>Education: '.$Education.'</li>';
$mail->addAttachment($qrCodeValueInput);
I tried everything, but due to my limited knowledge and general hate to ChatGPT (I just don’t think that its the best tool that will do everything for me without me properly understanding how everything works) I have no results – email is not even sent to the receiver. In error log it states that the addAttachment function is faulty.
2
Answers
You need to render it
If you generate the QR Code in a Canvas-element, you can use the
.toDataURL('image/png')
to get the canvas as a PNG.https://github.com/cheprasov/js-qrcode
Stackoverflow: Capture HTML canvas as GIF/JPG/PNG/PDF?