JSPDF attach to email

JSPDF attach to email

davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
edited September 2020 in Free community support

I would like to be able to attach PDF to my emails that are sent on update of a table. This works outside of datatables but when I put them into the PHP file it causes the editor to stop working.

This works currently without the jsPDF

<?php


use PHPMailer\PHPMailer\PHPMailer;



require '../../../vendor/autoload.php';
include( "lib/DataTables.php" );


use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    Editor::inst( $db, 'id_request' )
        ->fields(

                Field::inst( 'ID' ),
                Field::inst( 'timestamp' )
                ->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
                ->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
                ->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
                Field::inst( 'email' ),
                Field::inst( 'request_type' ),
                Field::inst( 'primary_location' ),
                Field::inst( 'employee_name' ),
                Field::inst( 'employee_role' ),
                Field::inst( 'emailGroups' ),
                Field::inst( 'team' ),
                Field::inst( 'last_4_soc' ),
                Field::inst( 'emp_mobile_number' ),
                Field::inst( 'shirt_size' ),
                Field::inst( 'district_name' ),
                Field::inst( 'birthdate_mmdd' ),
                Field::inst( 'default_password' ),
                Field::inst( 'fios_id' ),
                Field::inst( 'email_id' ),
                Field::inst( 'native_worker_id' ),
                Field::inst( 'ssoid' ),
                Field::inst( 'datascape_id' ),
                Field::inst( 'fios_background_check' ),
                Field::inst( 'created_by' ),
                Field::inst( 'complete' ),
                Field::inst( 'notes' ),
                Field::inst( 'acheck_file_number' ),
                Field::inst( 'modified' )
                ->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
                ->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
                ->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
                Field::inst( 'startDate' ),

        )

        ->on('postEdit', function ($editor, $id, $values, $row) {
            if ($row['complete'] == 'Yes') {
            $emailAddress = $row['email'];
            $mail = new PHPMailer;
            $mail->setFrom('support@.net', 'ID Request');
            $mail->addReplyTo('noreply@.net', 'No Reply');
            #$mail->addAddress(strval($emailAddress));
            $mail->addCC('davidm@.net', 'David Morin');
            $mail->Subject = 'ID Request Complete for '.$row['employee_name'];
            $mail->msgHTML('<b>RMA');
                if ($values['fios'] == 'Yes'){


                    $mail->Body = "
                        <html>
                         <head>
                                <style>
                                     .alnright { text-align: right; }
                                </style>
                         </head>
                         <body>
                                <img src='https://dev..net/assets/logo.png'><br />
                                Fios ID Complete
                                <p>
                                 Your ID request for <b>".$row['employee_name']."</b> is complete.
                                </p>
                                <p>
                                 Users Fios ID is ".$row['fios_id']."
                                 </p>
                                 <p>
                                 Notes: ".$row['notes']."
                                 </p>
                                 <p>
                                     <b>VZU Training</b>
                                     <ul>
                                    <li>    URL to access training: https://vzu.verizon.com </li>
                                    <li>    The username is your Optix ID (Valid 24 hours after you receive it) </li>
                                    <li>    The default initial password is: ! </li>
                                    <li>    This password is also used anytime an ID is deleted and then reactivated </li>
                                    <li>    If you have any issues logging in, please send an email to vzupasswordreset@verizon.com </li>
                                        </ul>

                                        <b>ID Activation</b>
                                        <ul>
                                    <li>    ID Requests will be in the queue until compliance training is complete </li>
                                    <li>    The day after training is complete, the ID will be fully activated and an activation email will be sent. </li>
                                    <li>    If background checks are required, they must be completed before the rep begins selling. </li>
                                    <li>    Emails will be sent to the user and supervisor that were provided when created. </li>
                                    <li>    Contact DSS.Optix.ID.Support@verizon.com with any ID related issues. </li>
                                    </ul>
                                    </p>
                                 <a href='http://portal.net'>Portal Access</a>
                                 <p>

                                 </p>
                         </body>
                    </html>";



                }else{
        $mail->Body = "
                <html>
                 <head>
                        <style>
                             .alnright { text-align: right; }
                             .divBorder { border: 1px solid black; width: 600px; box-shadow: 2px 2px 5px #333; }
                             .unTable { border: 1px solid black; width: 600px; box-shadow: 2px 2px 5px #333; }
                             .heading {font-size: 15px; font-weight: bold;}
                             .unTD {align: right; width: 150px;}
                        </style>
                 </head>
                 <body>
                        <img src='https://.net/assets/logo.png'><br />
                        ID Request Completed
                        <p>
             Your ID request for <b>".$row['employee_name']."</b> is complete.
                        </p>
                        <p>
                         You can visit the portal and download or view the employees information.
                         </p>
                         <p>
                         Notes: ".$row['notes']."
                         </p>
                         <a href='http://.net'>Access</a>
                         <p>
                         <div id='userInfo' >
                             <hr class='userLoginHR'>
                         <div class='heading'>
                         <center><img src='https://portal..net/idRequest/imlogo.png'></center>
                           <h3> New ID Information </h3>
                          <span class='heading'> Welcome aboard <span id='name'> ".$row['employee_name']."</span>. Below is your login information for all systems. If you have any issues please reach out to support@.net or call 888888</span>
                         </div><br />

                             <br>
                             <div class='divBorder'>
                             <b>Important Links:</b>
                             <ul>
                                <li> Omni: https://sposstore.verizonwireless.com/indirect </li>
                                <li> Intranet: https://my..net </li>
                                <li> EMail: https://mail..net </li>
                                <li> Calendar: https://calendar..net </li>
                                <li> Verizon Website: https://.com</li>
                                <li> IM Website: https://.net </li>
                                <li> Promo Site: https://unboxdindirect..com/Agents </li>
                                <li> Verizon Resources: https://docs.google.com/spreadsheets/d/1cT_4Hvr0jOUDJFg5hluZQY0R5-n4aOPd23zRFDLDhZc/edit#gid=0</li>
                                <li> IM Support: support@.net | https://my..net/helpdesk </li>
                                </ul>
                                </div>
                         </div>
                         </p>



                 </body>
            </html>";

}
            $mail->send();

}
                })


    ->debug( true )
        ->process( $_POST )
        ->json();

Now I need to include this script

<script>
     var d = new Date();
     month = '' + (d.getMonth() + 1),
     day = '' + d.getDate(),
     year = d.getFullYear();
     var n = d.getTime();
     var now = new Date();
     var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
     var formattedDate = now.getFullYear() + months[now.getMonth()] + now.getDate();
     var name = document.getElementById('name');
     var name2 = name.innerHTML;

    var pdf = new jsPDF('p', 'pt', 'letter');
    source = $('.userinfo')[0];
    specialElementHandlers = {
        '#bypassme': function (element, renderer) {
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 522
    };

    pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        pdf.save(name2 + ' Login Information.pdf');
        var name = name2 ;
        var pdf2 = btoa(pdf.output());

          $.ajax({
            method: "POST",
            url: "upload.php",
            data: {data: pdf2, name: name},
          }).done(function(data){
             console.log(data);
          });


    }, margins)

</script>

If I insert this into my working script it breaks.

How can I include this within my current setup?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    In what way does it break - fo you get an error message?

    I also don't really see how you are attempting to get the PDF file into the e-mail? There doesn't appear to be a call to PHPMailer's addAttachment method? Or any attempt to use the uploaded file. The PDF file doesn't appear to be sent to the server on row edit either - it looks like it is just created and sent to an Editor PHP script that doesn't use it.

    I'm also not clear on why you'd generate the PDF on the client-side, rather than the server, but that might just be based on information I don't have.

    Allan

This discussion has been closed.