[使用PHPMailer和html2pdf通过电子邮件发送PDF数据

问题描述 投票:0回答:2

我正在尝试通过电子邮件发送PDF或PNG,似乎没有任何效果。以下是我的LAST尝试,我已经阅读了SO上的每一篇文章,但都没有发现建议的内容,有人可以帮忙吗?我使用的是PHPMailer,html2pdf和html2canvas,它们都可以在单击时生成正确的文档,只是将它们发送到php mailer却无法正常工作。我正在获取无法打开的文档...以下是我的最后尝试...使用file_get_contents方法,我得到0大小的数据。

PDF尝试:

var element = document.getElementById('printableArea');
// var opt = {  
//      filename:     'nalog.pdf'
// };   
html2pdf().from(element).toPdf().output('datauristring').then(function (pdfAsString) {
    pdfcontent = pdfAsString.replace(/&/,"%26").replace(/=/,"%3D");
     var x = new XMLHttpRequest();
     var url = "xxx/mail.php";
     x.open("POST", url, true);
     x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     x.send("data="+pdfcontent);
console.log(pdfcontent);
        });

PHP:

$mail->AddStringAttachment($_POST['data'],"nalog.pdf"); 

PNG尝试:

html2canvas($('#printableArea')[0], {
  width: 1200
}).then(function(canvas) {
    var data = canvas.toDataURL("image/png");
    pdfcontent = data.replace(/&/,"%26").replace(/=/,"%3D");
    var x = new XMLHttpRequest();
    var url = "xxx/mail.php";
    x.open("POST", url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    x.send("data="+pdfcontent);
    console.log(pdfcontent);
});

PHP:

$mail->AddStringAttachment($_POST['data'],"nalog.png"); 

编辑:为了更新我的问题,我从anwser提出了一些建议,并尝试对其进行调试,但没有任何帮助。我制作了PHP file_put_contents()并使用控制台日志对其进行了编辑,它是相同的数据。

还有其他建议吗?

javascript php phpmailer html2canvas html2pdf
2个回答
0
投票

[我的猜测是问题出在传输期间,因为URI无法很好地处理二进制数据-可能有NUL字符,谁知道那里还有什么,这些字符会截断或弄乱数据。尝试像这样发送此数据(作为原始文本,而不是URL编码的文本):

    var x = new XMLHttpRequest();
    x.open("POST", "xxx/mail.php", true);
    x.setRequestHeader("Content-type", "text/plain; charset=utf-8");
    x.send( new Uint8Array(pdfcontent) );

然后,在服务器端,您需要像下面这样提取原始数据:

   $data = file_get_contents('php://input');

使用$ _POST将不起作用,它应该为空。

如果仍然生成错误的文件,则可能不是传输问题。尝试将任何console.log打印并粘贴的文件复制并粘贴到文件中,然后查看是否可以打开(即使数据有效,由于使用二进制数据,它也可能不起作用,但是如果确实有效,那么效果很好-在至少我们可以从根本上排除问题)。然后尝试使用file-put-contents(https://www.php.net/manual/en/function.file-put-contents.php)将数据保存在服务器端,并查看是否生成了有效文件。如果是这样,则在发送电子邮件时会出现问题。

在任何情况下,您都应该真正在服务器端生成文件-所有这些来回的操作效率极低,而且非常不安全。例如,您可以使用FPDF(http://www.fpdf.org/)使用PHP从头开始创建PDF文件,然后在此处通过电子邮件发送。是的,由于您需要分别生成HTML和PDF,因此它使输出工作加倍,但是从长远来看,这是值得的。也许您不需要在此设置中完全生成HTML?您是否仅出于通过电子邮件发送HTML的目的而生成HTML,还是真的需要将其呈现给用户?


0
投票

[几乎放弃之后,终于让它工作了。它是这里链接和建议的几件事的组合。github html2pdf上的帖子也有所帮助。

我将其发布在这里,因为没有一个示例对我有用,我花了两天时间才找到对我和我感兴趣的东西。希望它可以帮助某人。

window.onload = function pdfDivload (){
let el = document.getElementById('printableArea');
let opt = {
    margin:       1,
    filename:     'myfile.pdf',
    image:        { type: 'jpeg', quality: 0.98 },
    html2canvas:  { scale: 2 },
    jsPDF:        { unit: 'in', format: 'A4', orientation: 'portrait' }
};

html2pdf().set( opt ).from( el ).toPdf().output('datauristring').then(function( pdfAsString ) {
    let data = {
        'fileDataURI': pdfAsString,
    };
    $.post( "../prog/mail.php", data);
    console.log( data );
} );
};

PHP:

  if (isset($_POST['fileDataURI'])) {

                $pdfdoc         = $_POST['fileDataURI'];

            $b64file        = trim( str_replace( 'data:application/pdf;base64,', '', $pdfdoc ) );
            $b64file        = str_replace( ' ', '+', $b64file );
            $decoded_pdf    = base64_decode( $b64file );
            //file_put_contents( $attachment, $decoded_pdf );

            $mail = new PHPMailer;
            $mail->setFrom( '[email protected]', 'website' );
            $mail->addAddress( '[email protected]', 'OdedTa' );
            $mail->Subject  = 'First PHPMailer Message';
            $mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
            $mail->addStringAttachment($decoded_pdf, "nalog.pdf");
            $mail->isHTML( true );
            $mail->send();
© www.soinside.com 2019 - 2024. All rights reserved.