From 2de2d236f67485bb1e1adc4853346e41d665a0ca Mon Sep 17 00:00:00 2001 From: Dharmrajsinh Jadeja Date: Wed, 26 Jun 2024 23:12:45 +0530 Subject: [PATCH] Email Body Bug Fix Fixed email body message where participant name as not getting updated in each loop. --- functions.php | 290 ++++++++++++++++++++++++++------------------------ 1 file changed, 149 insertions(+), 141 deletions(-) diff --git a/functions.php b/functions.php index 449cb83..be5ef3a 100644 --- a/functions.php +++ b/functions.php @@ -1,118 +1,126 @@ Reading Data
'; - clearstatcache(); - $h = fopen($file, "r"); - while (($data = fgetcsv($h, 1000, ",")) !== FALSE) { - $cert_data[] = $data; - } - fclose($h); - echo 'Generating PDFs
'; - generate_certificate($cert_data,$input_data); +function read_data( $file, $input_data ) { + $GLOBALS['bgr_image'] = $input_data['bg_image']; + clearstatcache(); + $h = fopen( $file, 'r' ); + while ( ( $data = fgetcsv( $h, 1000, ',' ) ) !== false ) { + $cert_data[] = $data; + } + fclose( $h ); + generate_certificate( $cert_data, $input_data ); } -add_action('read_cert_data','read_data','',2); +add_action( 'read_cert_data', 'read_data', '', 2 ); -// Plugin dir path -$path = dirname(__FILE__); -// Including TCPDF library -require($path.'/library/TCpdf/tcpdf.php'); -// Custom class to set background image +// Plugin dir path. +$path = dirname( __FILE__ ); +// Including TCPDF library. +require $path . '/library/TCpdf/tcpdf.php'; +// Custom class to set background image. class MYPDF extends TCPDF { - //Page header - public function Header() { - // get the current page break margin - $bMargin = $this->getBreakMargin(); - // get current auto-page-break mode - $auto_page_break = $this->AutoPageBreak; - // disable auto-page-break - $this->SetAutoPageBreak(false, 0); - $this->SetMargins(0,0,0); - // set bacground image Image( $file, $x = '', $y = '', $w, $h, $type = '', $link = '', $align = '', $resize = false, $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border, $fitbox = false, $hidden = false, $fitonpage = false, $alt = false, $altimgs = array()) - $this->Image($GLOBALS['bgr_image'], 0, 0, 297,210, 'JPG', '', '', false,300, '', false, false, 0, false, false, true); - // restore auto-page-break status - $this->SetAutoPageBreak($auto_page_break, $bMargin); - $this->SetMargins(0,85,0); - // set the starting point for the page content - $this->setPageMark(); - } + // Page header. + public function Header() { + // get the current page break margin. + $bMargin = $this->getBreakMargin(); + // get current auto-page-break mode. + $auto_page_break = $this->AutoPageBreak; + // disable auto-page-break. + $this->SetAutoPageBreak( false, 0 ); + $this->SetMargins( 0, 0, 0 ); + // set bacground image Image( $file, $x = '', $y = '', $w, $h, $type = '', $link = '', $align = '', $resize = false, $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border, $fitbox = false, $hidden = false, $fitonpage = false, $alt = false, $altimgs = array()) + $this->Image( $GLOBALS['bgr_image'], 0, 0, 297, 210, 'JPG', '', '', false, 300, '', false, false, 0, false, false, true ); + // restore auto-page-break status. + $this->SetAutoPageBreak( $auto_page_break, $bMargin ); + $this->SetMargins( 0, 85, 0 ); + // set the starting point for the page content. + $this->setPageMark(); + } } -function generate_certificate($cert_data,$input_data){ - $path = dirname(__FILE__); - // Extend the TCPDF class to create custom Header and Footer - foreach ($cert_data as $key => $value) { - if ($key != 0) { - // User defined pdf Customization - $general_font_size = ($input_data['general_font_size'] == null) ? 14 : $input_data['general_font_size']; - $attendee_font_size = ($input_data['attendee_font_size'] == null) ? 50 : $input_data['attendee_font_size']; - $event_title_font_size = ($input_data['event_title_font_size'] == null) ? 18 : $input_data['event_title_font_size']; +function generate_certificate( $cert_data, $input_data ) { + $path = dirname( __FILE__ ); + // Extend the TCPDF class to create custom Header and Footer. + foreach ( $cert_data as $key => $value ) { + if ( 0 !== $key ) { + // User defined pdf Customization. + $general_font_size = ( null === $input_data['general_font_size'] ) ? 14 : $input_data['general_font_size']; + $attendee_font_size = ( null === $input_data['attendee_font_size'] ) ? 50 : $input_data['attendee_font_size']; + $event_title_font_size = ( null === $input_data['event_title_font_size'] ) ? 18 : $input_data['event_title_font_size']; + $start_sub_line = empty( $input_data['start_sub_line'] ) ? 'This is to recognize' : $input_data['start_sub_line']; + $second_sub_line = empty( $input_data['second_sub_line'] ) ? 'For attending the webinar on' : $input_data['second_sub_line']; - // create new PDF document - $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); - // set document information - $pdf->SetCreator('Ananda Vak Sols LLP'); - $pdf->SetAuthor('Ananda Vak Sols LLP'); - $pdf->SetTitle('Certificate of Attendance - '.$value[1]); - $pdf->SetSubject('Certificate of Attendance - '.$value[1]); - // set default monospaced font - $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); - // set margins - $pdf->SetHeaderMargin(0); - $pdf->SetFooterMargin(0); - $pdf->SetMargins(50,85,50, true); - // remove default footer - $pdf->setPrintFooter(false); - // set auto page breaks - $pdf->SetAutoPageBreak(false, 0); - // set image scale factor - $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); - // --------------------------------------------------------- - // add a page - $pdf->AddPage('L','A4'); - $pdf->Image($input_data['org_image'], 112, 31, 67.01,15.44, 'PNG', '', '', false,300, '', false, false, 0, false, false, false); - // set font - $pdf->SetFont('Helvetica', '', $general_font_size); - // Print a text - $pdf->Cell(0, 5, 'This is to recognize', 0, 1, 'C', 0, '', 0); - // set font - $pdf->SetFont('Helvetica', '', $attendee_font_size); - // Attendars name - $pdf->Cell(0, 5, $value[0], 0, 1, 'C', 0, '', 0); - // set font - $pdf->SetFont('Helvetica', '', $general_font_size); - $pdf->Cell(0, 5, 'For attending the webinar on', 0, 1, 'C', 0, '', 0); - // set font - $pdf->SetFont('Helvetica', 'B', $event_title_font_size); - // Event Title - $pdf->MultiCell(0, 5, $value[1], 0, 'C', 0, 1, '', '', true); - // set font - $pdf->SetFont('Helvetica', '', $general_font_size); - // Event Date - $pdf->Cell(0, 8, $value[2], 0, 2, 'C', 0, '', 0); - if (!file_exists($path.'/certificate/')) { - mkdir($path.'/certificate/', 0777, true); - } - $pdf_file_path = $path.'/certificate/'.$value[0].'-'.date('ymdHis').'.pdf'; - $pdf_file_name = basename($pdf_file_path); - $pdf->Output($pdf_file_path,'F'); - if (file_exists($pdf_file_path)) { - echo 'File Exists.
Sending Mail.
'; - // Email Details - $to = $value[3]; - $sub = 'Certificate of Attendance for '.$value[0]; - // checking if email body is entered - if(!empty($input_data['email_body'])){ - // Replace the custom variables with their values - $input_data['email_body'] = str_replace("{{variable1}}", $value[0], $input_data['email_body']); - $input_data['email_body'] = str_replace("{{variable2}}", $value[1], $input_data['email_body']); - $input_data['email_body'] = str_replace("{{variable3}}", $value[2], $input_data['email_body']); - $msg = htmlspecialchars_decode($input_data['email_body']); - } - else{ - // Default MSG - $msg = ' + if ( strlen( $start_sub_line ) > 50 ) { + $start_sub_line = substr( $start_sub_line, 0, 50 ) . '...'; + } + if ( strlen( $second_sub_line ) > 50 ) { + $second_sub_line = substr( $second_sub_line, 0, 50 ) . '...'; + } + + // create new PDF document. + $pdf = new MYPDF( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false ); + // set document information. + $pdf->SetCreator( 'Ananda Vak Sols LLP' ); + $pdf->SetAuthor( 'Ananda Vak Sols LLP' ); + $pdf->SetTitle( 'Certificate of Attendance - ' . $value[1] ); + $pdf->SetSubject( 'Certificate of Attendance - ' . $value[1] ); + // set default monospaced font. + $pdf->SetDefaultMonospacedFont( PDF_FONT_MONOSPACED ); + // set margins. + $pdf->SetHeaderMargin( 0 ); + $pdf->SetFooterMargin( 0 ); + $pdf->SetMargins( 50, 85, 50, true ); + // remove default footer. + $pdf->setPrintFooter( false ); + // set auto page breaks. + $pdf->SetAutoPageBreak( false, 0 ); + // set image scale factor. + $pdf->setImageScale( PDF_IMAGE_SCALE_RATIO ); + // --------------------------------------------------------- + // add a page. + $pdf->AddPage( 'L', 'A4' ); + $pdf->Image( $input_data['org_image'], 112, 31, 67.01, 15.44, 'PNG', '', '', false, 300, '', false, false, 0, false, false, false ); + // set font. + $pdf->SetFont( 'Helvetica', '', $general_font_size ); + // Print a text. + $pdf->Cell( 0, 5, $start_sub_line, 0, 1, 'C', 0, '', 0 ); + // set font. + $pdf->SetFont( 'Helvetica', '', $attendee_font_size ); + // Attendars name. + $pdf->Cell( 0, 5, $value[0], 0, 1, 'C', 0, '', 0 ); + // set font. + $pdf->SetFont( 'Helvetica', '', $general_font_size ); + $pdf->Cell( 0, 5, $second_sub_line, 0, 1, 'C', 0, '', 0 ); + // set font. + $pdf->SetFont( 'Helvetica', 'B', $event_title_font_size ); + // Event Title. + $pdf->MultiCell( 0, 5, $value[1], 0, 'C', 0, 1, '', '', true ); + // set font. + $pdf->SetFont( 'Helvetica', '', $general_font_size ); + // Event Date. + $pdf->Cell( 0, 8, $value[2], 0, 2, 'C', 0, '', 0 ); + if ( ! file_exists( $path . '/certificate/' ) ) { + echo 'Creating file if file not exists
'; + mkdir( $path . '/certificate/', 0777, true ); + } + $pdf_file_path = $path . '/certificate/' . $value[0] . '-' . date( 'ymdHis' ) . '.pdf'; + $pdf_file_name = basename( $pdf_file_path ); + $pdf->Output( $pdf_file_path, 'F' ); + if ( file_exists( $pdf_file_path ) ) { + echo 'File Exists.
Sending Mail.
'; + // Email Details. + $to = $value[3]; + $sub = 'Certificate of Attendance for ' . $value[0]; + // checking if email body is entered. + if ( ! empty( $input_data['email_body'] ) ) { + // Replace the custom variables with their values. + $email_body = $input_data['email_body']; + $email_body = str_replace( '{{variable1}}', $value[0], $email_body ); + $email_body = str_replace( '{{variable2}}', $value[1], $email_body ); + $email_body = str_replace( '{{variable3}}', $value[2], $email_body ); + $msg = htmlspecialchars_decode( $email_body ); + } else { + // Default MSG. + $msg = '
@@ -140,41 +148,41 @@ function generate_certificate($cert_data,$input_data){
@@ -124,8 +132,8 @@ function generate_certificate($cert_data,$input_data){
-

Dear '.$value[0].',

-

EFY Group would like to thank you for attending the webinar titled '.$value[1].' on '.$value[2].'. We hope you will be able to benefit from the knowledge gained during this webinar, and look forward to seeing you at the next event.

+

Dear ' . $value[0] . ',

+

EFY Group would like to thank you for attending the webinar titled ' . $value[1] . ' on ' . $value[2] . '. We hope you will be able to benefit from the knowledge gained during this webinar, and look forward to seeing you at the next event.

We truly appreciate your support.

Thank you!

'; - } - $headers[] = 'From: '.$input_data['sender_name'].' <'.$input_data['sender_email'].'>'; - $headers[] = 'Content-Type: text/html; charset=UTF-8'; - if(send_mail($to,$sub,$msg,$headers,$pdf_file_path)){ - echo 'Mail sent successfully
'; - } - } - echo $value[0] . ' PDF Generated -> Open

'; - } - } + } + $headers[] = 'From: ' . $input_data['sender_name'] . ' <' . $input_data['sender_email'] . '>'; + $headers[] = 'Content-Type: text/html; charset=UTF-8'; + if ( send_mail( $to, $sub, $msg, $headers, $pdf_file_path ) ) { + echo 'Mail sent successfully
'; + } + } + echo $value[0] . ' PDF Generated -> Open

'; + } + } } -function send_mail($to,$sub,$msg,$headers,$attachment){ - return wp_mail($to,$sub,$msg,$headers,$attachment); +function send_mail( $to, $sub, $msg, $headers, $attachment ) { + return wp_mail( $to, $sub, $msg, $headers, $attachment ); } // Clear Data function. This will be called via AJAX call -function cleardata(){ - echo "Clearing Data of certificates and generated PDFs"; - $path = dirname(__FILE__); - // Clearing Certificate data - $files = glob($path.'/data/*'); // get all file names - foreach($files as $file){ // iterate files - if(is_file($file)) { - unlink($file); // delete file - } - } - // Clearing PDFs - $files = glob($path.'/certificate/*'); // get all file names - foreach($files as $file){ // iterate files - if(is_file($file)) { - unlink($file); // delete file - } - } - wp_die(); +function cleardata() { + echo 'Clearing Data of certificates and generated PDFs'; + $path = dirname( __FILE__ ); + // Clearing Certificate data + $files = glob( $path . '/data/*' ); // get all file names + foreach ( $files as $file ) { // iterate files + if ( is_file( $file ) ) { + unlink( $file ); // delete file + } + } + // Clearing PDFs + $files = glob( $path . '/certificate/*' ); // get all file names + foreach ( $files as $file ) { // iterate files + if ( is_file( $file ) ) { + unlink( $file ); // delete file + } + } + wp_die(); } -add_action('wp_ajax_cleardata', 'cleardata'); -add_action('wp_ajax_nopriv_cleardata', 'cleardata'); +add_action( 'wp_ajax_cleardata', 'cleardata' ); +add_action( 'wp_ajax_nopriv_cleardata', 'cleardata' );