【问题标题】:I am trying to send form data to pdf and attach with mail我正在尝试将表单数据发送到 pdf 并通过邮件附加
【发布时间】:2016-04-28 09:52:59
【问题描述】:

我正在使用下面的代码并且它正在工作。但是附加的代码不起作用,并且发送邮件失败。会不会是标头不对?

这里的$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);
       }

【问题讨论】:

  • 你用PHPMailer标记了这个问题,但你没有使用它。您应该这样,您就不必担心如何正确构建附件。通常,一次解决一个问题 - 在尝试发送之前检查您的 PDF 版本是否正常工作。另外,看看file_put_contents,它比fopen 更简单。
  • 是的,我的 pdf 版本正在运行,但我在发送邮件附件时遇到问题...
  • “附加代码”如何工作而“下面的代码”不能工作?
  • 如果它正常工作,它将很容易受到电子邮件注入。
  • 我是说表单数据被转换成PDF,但我还没有找到如何将pdf缓冲数据发送到邮件的解决方案

标签: php fpdf html-to-pdf


【解决方案1】:

您应该使用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';
}

我稍微修改了上面分享的链接中的示例。

正如 Synchro 建议的那样,您可以使用 addStringAttachment 方法,以跳过整个将 PDF 写入文件的步骤并直接从内存中发送。

【讨论】:

  • 如果你使用PHPMailer的addStringAttachment方法,你可以跳过整个将PDF写入文件的步骤,直接从内存中发送。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多