Php 我正在尝试将表单数据发送到pdf并随邮件附上

Php 我正在尝试将表单数据发送到pdf并随邮件附上,php,fpdf,html-to-pdf,Php,Fpdf,Html To Pdf,我使用下面的代码,它的工作。但是,附加的代码不起作用,发送邮件失败。这可能与错误的标题有关吗 这里,$htmlTable变量用于收集表单post值,并使用html-to-pdf函数将其转换为pdf。 但是邮件不是用pdf发送的 $pdf->WriteHTML2("<br><br><br>$htmlTable"); $pdf->SetFont('Arial','B',6); //Create PDF file with data $semi_rand

我使用下面的代码,它的工作。但是,附加的代码不起作用,发送邮件失败。这可能与错误的标题有关吗

这里,
$htmlTable
变量用于收集表单post值,并使用html-to-pdf函数将其转换为pdf。 但是邮件不是用pdf发送的

$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
//Create PDF file with data
$semi_rand = md5(time());
$pdf1 = $htmlTable;

 $file = $semi_rand . ".pdf"; 
 $handle = fopen($file, 'w+');
 fwrite($handle, $pdf);   
 fclose($handle);
 // Additional headers
   $subject = "User Form";
    $content = chunk_split(file_get_contents($file));
       $uid = md5(uniqid(time()));  //unique identifier

       //standard mail headers
       $header = "From: ".$from."\r\n";
       $header .= "Reply-To: ".$_POST['email']. "\r\n";
       $header .= "MIME-Version: 1.0\r\n";


       //declare multiple kinds of email (plain text + attch)
       $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
       $header .="This is a multi-part message in MIME format.\r\n";

       //plain txt part

      //$header .= "--".$uid."\r\n";
     // $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
     // $header .= "Content-Transfer-Encoding: bit\r\n\r\n";
      //$header .= 'sample'. "\r\n\r\n";


       //attch part
       $header .= "--".$uid."\r\n";
       $header .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n";
       $header .= "Content-Transfer-Encoding: base64\r\n";
       $header .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
       $header .= $content."\r\n\r\n";  //chucked up 64 encoded attch
        $success = mail($to,$subject,'PFA',$header);
       if (!$success) {
      echo "Mail to " . $to . " failed .";
       }else {
      echo "Success : Mail was send to " . $to ;
      print_r ($header);
       }
$pdf->WriteHTML2(



$htmlTable”); $pdf->SetFont('Arial','B',6); //创建包含数据的PDF文件 $semi_rand=md5(time()); $pdf1=$htmlTable; $file=$semi_rand。“.pdf”; $handle=fopen($file'w+'); fwrite($handle,$pdf); fclose($handle); //附加标题 $subject=“用户表单”; $content=chunk\u split(file\u get\u contents($file)); $uid=md5(uniqid(time())//唯一标识符 //标准邮件头 $header=“From:”.$From.\r\n”; $header.=“回复:”.$\u帖子['email']。“\r\n”; $header.=“MIME版本:1.0\r\n”; //声明多种电子邮件(纯文本+附件) $header.=“内容类型:多部分/混合;边界=\”.$uid.\“\r\n\r\n”; $header.=“这是MIME格式的多部分消息。\r\n”; //普通txt部件 //$header.=“-”$uid.\r\n”; //$header.=“内容类型:文本/普通;字符集=iso-8859-1\r\n”; //$header.=“内容传输编码:位\r\n\r\n”; //$header.='sample'。“\r\n\r\n”; //附件部分 $header.=“-”$uid.\r\n”; $header.=“内容类型:应用程序/八位字节流;名称=\”“.$file.\”\r\n”; $header.=“内容传输编码:base64\r\n”; $header.=“内容处置:附件;文件名=\”.$file.\“\r\n\r\n”; $header.=$content.“\r\n\r\n”//抛出64编码的攻击 $success=mail($to,$subject,'PFA',$header); 如果(!$success){ 回显“邮件收件人”。$to。“失败。”; }否则{ echo“成功:邮件已发送至”。$to; 打印(页眉); }
您应该使用class,因为附加文件并通过电子邮件发送更简单

发送带有附件的电子邮件的示例可以是:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Attachment is here';
$mail->Body    = 'Here goes the attachment;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

您将此问题标记为,但您没有使用它。您应该这样做,那么您就不必担心如何正确地构建附件。一般来说,一次解决一个问题-在发送之前检查PDF版本是否正常工作。另外,看看
文件内容
,这比用
fopen
伪造要简单得多。是的,我的pdf版本正在工作,但我在用邮件发送附件时遇到了问题……如何才能使用“附加代码”和“下面的代码”不起作用?如果它起作用,它将很容易受到电子邮件注入的影响。我是说表单数据已转换为PDF,但我还没有找到如何将PDF缓冲数据发送到邮件的解决方案如果使用PHPMailer的
addStringAttachment
方法,您可以跳过将PDF写入文件的整个步骤,直接从内存发送。