【问题标题】:phpmailer attatch post data pdf file without savephpmailer附加post数据pdf文件而不保存
【发布时间】:2021-11-19 06:26:06
【问题描述】:

你好,我正在尝试通过 phpmailer 发送带有输入表单 javascript jspdf 的邮件

邮件发送正常,但如果我通过原始 php 文件获取 $_POST['data'],则文件已损坏。 我如何将带有 $_POST['data'] 的 jspdf 文件传送到 phpmailer?

我尝试了很多方法,但没有多大帮助。 感谢您阅读本文

form_input.php

  var create_pdf = document.getElementById("create_pdf");
  create_pdf.addEventListener('click', function (event) {
    html2canvas($('#pdf_wrap')[0] ,{    
      //logging : true,     
      //proxy: "html2canvasproxy.php",
      allowTaint : true,    
      useCORS: true,        
      scale : 2         
    }).then(function(canvas) {  
      
        var imgData = canvas.toDataURL('image/jpeg');           
        var doc = new jsPDF("p", "px");
        var options = {
             pagesplit: true
        };

        var imgWidth = 210; 
        var pageHeight = 295;  
        var imgHeight = canvas.height * imgWidth / canvas.width;
        var heightLeft = imgHeight;

        var doc = new jsPDF('p', 'mm');
        var position = 0;

        doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;

        while (heightLeft >= 0) {
          position = heightLeft - imgHeight;
          doc.addPage();
          doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
          heightLeft -= pageHeight;
        }


        //Send mail on other php   
        $.post("mail_send.php", 
        {
            data: doc.output('datauristring')
        }, function () {}).done(function() {/*SOME CODE*/});    
       // doc.save( 'file.pdf');

    });
  });

ma​​il_send.php

require 'plugin/PHPMailer/class.phpmailer.php';
require 'plugin/PHPMailer/class.smtp.php';
require 'plugin/PHPMailer/PHPMailerAutoload.php';

if(!empty($_POST['data']))
{
    echo $_POST['data'];

    $mail = new PHPMailer(true);
    try {
        //Server settings
        $mail->SMTPDebug = false;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp.test.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'smtpid@dot.com';                     // SMTP username
        $mail->Password   = 'password';                               // SMTP password
        $mail->Port       = 465;                   
        $mail->SMTPAuth = true;
        $mail->SMTPKeepAlive = true;                  // TCP port to connect to
        $mail->SMTPSecure = 'ssl';
        $mail->setFrom('setmail@mail.com', 'Mailer');
        $mail->addAddress('user@mail.com', 'Joe User');     // Add a recipient
        $mail->CharSet = "utf-8";
        // ----------Attachments this doesn't work. delivered broken pdf file.
        $base = explode('data:application/pdf;base64,', $_POST['data']);
        $mail->addStringAttachment($base, 'pdfName.pdf');
        // -----------
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Body = $body;
        $mail->Subject = 'Here is the subject';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        $mail->send();
        echo 'Message has been sent';

        
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }

【问题讨论】:

    标签: php base64 phpmailer jspdf html2pdf


    【解决方案1】:

    您需要仔细检查您的数据,但我认为是问题所在

    $base = explode('data:application/pdf;base64,', $_POST['data']);
    $mail->addStringAttachment($base, 'pdfName.pdf');
    

    这不会产生 PDF 附件;它会生成一个标记为 PDF 的 base64 编码的 PDF 附件,然后 PHPMailer 将对其进行第二次 base64 编码,因为它无法知道您提供的数据。所以在附加之前解码PDF数据:

    $mail->addStringAttachment(base64_decode($base), 'pdfName.pdf');
    

    【讨论】:

    • 首先感谢您的评论,我已经尝试过了,但仍然返回损坏的 pdf 文件。
    • 如果您尝试过,请在问题中说明。现在按照我的建议进行操作并仔细检查您的数据,以确认您确实拥有哪些数据。与其通过电子邮件发送,不如将其保存到一个文件中并在其中检查:如果您有一个损坏的 PDF,通过电子邮件发送它不会改变任何东西,但它确实为出错添加了一种新方法,所以 首先检查您的数据。
    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多