Im currently working on a project with features like send certificate to email by using ajax, phpmailer and fpdf library. But when i click the send button its not sending and this error pop up inside the button "
Trying to access array offset on value of type bool
".
I tried this block of code to generate and send certificate to email
"
<?php
use PHPMailerPHPMailerPHPMailer;
if(isset($_POST['email_data'])){
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
require 'fpdf/fpdf.php';
require 'connection.php';
$output='';
foreach($_POST['email_data']as $row)
{
$image= imagecreatefrompng('D:/App Projects/Source/idonate/Admin/include/Certificate Template/certificate2.png');
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$font="D:/App Projects/Source/idonate/Admin/fonts/Roboto-Black.ttf";
$size =110;
$box = imagettfbbox($size, 0, $font, $row['donor_name']);
$text_width = abs($box[2]) - abs($box[0]);
$text_height = abs($box[5]) - abs($box[3]);
$image_width = imagesx($image);
$image_height = imagesy($image);
$x = ($image_width - $text_width) / 2;
$y = ($image_height + $text_height) / 2;
// add text
imagettftext($image, $size, 0, $x, $y, $black,$font, $row['donor_name']);
$file=time();
$file_path="download-certificate/".$file.".png";
$file_path_pdf= "download-certificate/".$file.".pdf";
imagepng($image,$file_path);
imagedestroy($image);
$pdf= new FPDF();
$pdf->AddPage('L','A5');
$pdf->Image($file_path,0,0,210,150);
$mail=new PHPMailer;
$mail->isSMTP();
$mail->Host= 'smtp.gmail.com';
$mail->SMTPAuth= true;
$mail->Username='[email protected]' ;
$mail->Password= 'mlytxekfgplnhsap';
$mail->SMTPSecure='ssl';
$mail->Port=465;
$mail->setFrom('[email protected]');
$mail->addAddress($row['donor_email']);
$mail->isHTML(true);
$mail->Subject= "Certificate";
$mail->Body= "This is certificate";
$mail->addStringAttachment($pdf->Output("S",'AcknowledgementReciept.pdf'), 'AcknowledgementReciept.pdf', $encoding = 'base64', $type = 'application/pdf');
$mail->AltBody='';
$sendEmail= $mail->send();
"
But when I add this code for connection and validation to ajax"
if($sendEmail["code"]==('400')){
$output .= html_entity_decode($sendEmail['full_error']);
}
}
if($output==''){
echo 'ok';
}else{
echo $output;
}
}
"Its not working but when a remove it its sending the email but the button is not disabled after success.
This is for the ajax"
$.ajax({
url:"http://localhost:3000/Admin/include/sendcerti.php" ,
method: "POST",
data: {email_data:email_data},
beforeSend:function(){
$('#'+donor_id).html('Sending...');
$('#' + donor_id).addClass('btn-danger');
},
success: function(data){
if (data == 'ok')
{
$('#' +donor_id).text("Success");
$('#' + donor_id).removeClass('btn-danger');
$('#' + donor_id).removeClass('btn-info');
$('#' + donor_id).addClass('btn-success');
}
else{
$('#' +donor_id).text(data);
}
$('#'+ donor_id).attr('disabled', false);
}
})
" I want to disable the button after success to prevent duplication of certificate
2
Answers
So I figured out how can I pass error and disable button to ajax first I modify this code and change it to true
Then I created if else to the PHP file to pass the validation to ajax https://api.jquery.com/category/forms/
Then for the success function I parse the data from $res variable.
This isn’t a complicated problem! First you’re doing this:
The send method returns a boolean value. So not surprisingly, when you try to access it as an array in code like
$sendEmail["code"]
, it will fail in exactly the way you are seeing.You are assuming (incorrectly) that PHPMailer returns things in a way that it does not, so update your code to do it correctly, looking at properties such as
ErrorInfo
.