Javascript mail function with attachments

I have a html page with a form, where the end user generates a pdf of the form by pressing a specific button. My code:

<button onclick="generatePDF()">PDF</button>
        <script>
            function generatePDF() 
            {
                const element = document.getElementById('container');/Container is where the form is located

                var filecommessa = document.getElementById("commessajava").value;
                var filedata = document.getElementById("datajava").value;
                var filerapporto = "Rapporto_";
                var fileunderscore = "_";
                var fileName = filerapporto+filecommessa+fileunderscore+filedata;
                console.log(fileName);

                var opt = {
                filename: fileName,
                jsPDF: { unit: 'in', format: 'A3', orientation: 'landscape' }
                };
                html2pdf()
                .from(element)
                .set(opt)
                .save();
                

            }
        </script>

I have another function, which allows the user to send an email to a customer.

<script>
            function sendMail() 
            {
                var mailcommessa = document.getElementById("commessajava").value;
                var maildata = document.getElementById("datajava").value;
                var mailrapporto = "Rapporto ";
                var space = " ";
                var mailsubject = mailrapporto+mailcommessa+space+maildata;

                var link = "mailto:"
                + "?cc="
                + "&subject=" + encodeURIComponent(mailsubject)
                + "&body=" + encodeURIComponent(document.getElementById('myText').value)
                ;
    
                window.location.href = link;
            }
        </script>

I would to know how can I attach the generated pdf to the email automatically, instead of downloading it and then attaching the downloaded file to the email. If this is not possible with the mailto function, other similar suggestions will be appreciated. Thanks for your help.

Hi @Browning2, actually not even this would work – you just can’t add attachments with mailto: links. You can only download the file to your server, and then include the (dynamically generated) download link in the email body.

If it’s not a large multi-page PDF file however, you might consider generating an image instead; this you could inline in the email body as a base64-encoded image source. It would then look like

// ...
+ '&body=' + encodeURIComponent('<img src="data:image/jpeg;base64,/...">')
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.