繁体   English   中英

以PDF格式作为附件填写的电子邮件数据

[英]Email data that was filled in PDF as Attachment

我有一个填充的PDF文件,提交后,该文件将通过Adobe Acrobat X中的“发送完成的pdf”选项发送完整的pdf到processpdf.php文件(请参见下面的代码),该代码使用$ HTTP_RAW_POST_DATA获取原始数据并将其通过电子邮件发送。 它可以通过电子邮件发送,我得到了pdf,但是PDF无法以chrome / acrobat或其他任何格式打开,因为它表明文件已损坏。 我在这里做错了什么?

我正在使用PHP5,Adobe Acrobat X,Safari 6

   <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    if(!isset($HTTP_RAW_POST_DATA)) {
        echo "The Application could not be sent. Please save the PDF and email it manually.";
        exit;
    }

    $email_from = "xxx@xxx.com";
    $email_subject = "Subject";
    $email_txt = "A Form has been sent from xxx.com. See
    attachment.";

    $email_to = "xxx@xxxxx.com";

    $headers = "From: ".$email_from;

    $semi_rand = md5(time());
    $mime_boundary = "----=_NextPart_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $email_message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n"
    .$email_txt. "\n\n";

    // This uses the function above as the Version of PHP on the server
    //does not have
    // it available.

    $pdf = $HTTP_RAW_POST_DATA;

    $data = chunk_split($pdf);

    $email_message .= "--{$mime_boundary}\n" .
    "Content-type: application/pdf;\n name=\"App.pdf\"\n" .
    "Content-Transfer-Encoding: quoted-printable\n" .
    "Content-Disposition: attachment;\n filename=\"App.pdf\"\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    $ok = mail($email_to, $email_subject, $email_message, $headers);

    if($ok) {
    echo ("The file was successfully sent!");
    } else {
    die("Sorry but the email could not be sent. Please go back and try
    again!");
    }
    ?>

我知道了!! 感谢您对phpmailer的建议,该建议似乎已经解决了该问题。

工作代码。 希望这可以帮助我从尝试解决此问题的8个多小时中受益的人们! 我不敢相信用PHP处理可填充PDF的文档很少。 使用PHPmailer类和以下代码

<?php
if(!isset($HTTP_RAW_POST_DATA)) {
    echo "The Application could not be sent. Please save the PDF and email it manually.";
    exit;
}
echo "<html><head></head><body><img src='loading.gif'>"; //Loading image

//Create PDF file with the filled data
$semi_rand = md5(time());
$pdf = $HTTP_RAW_POST_DATA;

 $file = $semi_rand . ".pdf"; 
 $handle = fopen($file, 'w+');
 fwrite($handle, $pdf);   
 fclose($handle);
//

require_once('class.phpmailer.php');

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.xxxxx.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
  $mail->Username   = "XXXX@XXXXXX.com";     // GMAIL username
  $mail->Password   = "XXXXX";               // GMAIL password
  $mail->AddAddress('XXXX@XXXX.com', 'First last');
  $mail->SetFrom('XXXX@XXXXXX.com', 'Application ');
  $mail->Subject = 'Subject';
  $mail->Body = 'Body of the e-mail';
  $mail->AddAttachment($file); // attachment
  ob_start(); $mail->Send(); ob_get_clean(); //Prevents SMTP responses

  unlink($file);  //delete the temporary pdf file then redirect to the success page
  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';    
  exit; 

//otherwise show errors
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
  unlink($file);  //doubley make sure the temp pdf gets deleted
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM