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

时间:2016-04-28 09:53:00

标签: php fpdf html-to-pdf

我正在使用以下代码并且它正在运行。但是附加的代码不起作用,并且发送邮件失败。可能是错误的标题吗?

此处$htmlTable变量用于收集表单后期值并使用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);
       }

1 个答案:

答案 0 :(得分:2)

您应该使用PHPMailer课程,因为它可以更简单地附加文件并通过电子邮件发送。

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

<?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';
}

我稍微修改了可以在我上面分享的链接中找到的示例。

正如同步建议您可以使用addStringAttachment方法,以便跳过将PDF写入文件步骤并直接从内存发送。