pdfHtml5 button to send table via PHPMailer

pdfHtml5 button to send table via PHPMailer

koppanmkoppanm Posts: 17Questions: 3Answers: 1
edited June 22 in Editor

Hi all,
I have partially succeeded with creating a send button to post filtered editor table data (using predefined search criteria) to phpmailer. The message comes thru, but the attachment file only contains "true".
Here is my code. Any help would be greatly appreciated.
Thanks,
Miklos

topStart: {
            buttons: [ 
                {
    extend: 'pdfHtml5',
    text: 'Send as PDF',
    title: 'DataTable Export',
    customize: function (doc) {
      // optional: customize PDF
    },
    action: function (e, dt, button, config) {
      let pdfDoc = window.pdfMake.createPdf(config);
      pdfDoc.getBlob(function (blob) {
        let formData = new FormData();
        formData.append("pdf", blob, "datatable.pdf");

        // send PDF to server via AJAX
        fetch("send_pdf_email_koppan.php", {
          method: "POST",
          body: formData,
        })
        .then(response => response.text())
        .then(result => alert("Email sent: " + result))
        .catch(error => alert("Error: " + error));
      });
    }
  },

Answers

  • allanallan Posts: 64,602Questions: 1Answers: 10,683 Site admin

    Sounds like a problem in the send_pdf_email_koppan.php file rather than the client-side. Does the POST request include the generated PDF data?

    Allan

  • koppanmkoppanm Posts: 17Questions: 3Answers: 1
    edited 8:26AM

    Dear Allan,
    Honestly, I do not know.
    Here is the <code>send_pdf_email_koppan.php</code> file:

    <?php
    require 'vendor/autoload.php'; // Composer: phpmailer/phpmailer
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    if (!isset($_FILES['pdf'])) {
        http_response_code(400);
        echo "No PDF received.";
        exit;
    }
    
    $tmpName = $_FILES['pdf']['tmp_name'];
    $originalName = $_FILES['pdf']['name'];
    $target = __DIR__ . '/' . $originalName;
    
    move_uploaded_file($tmpName, $target);
    
    // Send email with PHPMailer
    $mail = new PHPMailer(true);
    
    try {
        $mail->CharSet = "UTF-8";
        $mail->isSendmail();
        $mail->setFrom('no-reply@laparo.hu', 'GYN Team');
        $mail->addReplyTo('no-reply@laparo.hu', 'GYN Team');
        $mail->addAddress('name@icloud.com', 'Miklos Koppan');
        $mail->Subject = 'PDF tábla csatolva!';
        $mail->addCC('name@gmail.com', 'Viktoria Szanto');
        $mail->Body = 'PDF table export attached.';
        $mail->addAttachment($target);
    
        $mail->send();
        echo "PDF emailed successfully.";
    } catch (Exception $e) {
        echo "Mailer Error: {$mail->ErrorInfo}";
    }
    
  • allanallan Posts: 64,602Questions: 1Answers: 10,683 Site admin

    I would suggest checking the network inspector in your browser to make sure that the POST request has the PDF data. After that' I'd suggest checking that the file does get moved to the $target directory (you aren't deleting it, so it should still be present after the PHP file has completed its execution). See what that is and that it is a valid PDF. The result of that should hopefully inform you of what your next debugging action would be (i.e. if it isn't the PDF, trace back from there, etc).

    Allan

Sign In or Register to comment.