【问题标题】:FPDF email attachment and outlookFPDF 电子邮件附件和前景
【发布时间】:2013-04-08 09:04:03
【问题描述】:

我正在尝试使用 FPDF 并将生成的文件附加到电子邮件中。我看过这篇帖子Email PDF Attachment with PHP Using FPDF,并且在向我自己发送电子邮件并在 Thunderbird 中查看时给出的答案有效。这是给出的示例代码:

 <?php
 require('lib/fpdf/fpdf.php');

$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');

// email stuff (change data below)
$to = "myemail@example.com"; 
$from = "me@example.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "test.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
mail($to, $subject, $body, $headers);

?>

但是,当在 Outlook (2007) 中发送和查看时,它也会将邮件创建为附件,这与代码或 Outlook 有关吗/

任何帮助表示赞赏。

伊恩

【问题讨论】:

    标签: php pdf-generation attachment email-attachments fpdf


    【解决方案1】:

    IMO 将 这是一个 MIME 编码的消息。 作为部分的内容是没有意义的;通常这样的句子在第一个 MIME 分隔符之前,以告知没有 MIME 功能的邮件阅读器的任何用户以下几行代表什么。本质上,您的邮件包含两个文本部分,一个是纯文本(这是一个 MIME 编码的消息。)和一个 html(请参阅附件。)在 multipart/mixed 容器。如果是multipart/alternative,邮件阅读器会选择其中一个来显示,但这样读者可能会怀疑要显示什么。

    因此,我建议缩短

    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;
    

    $body = "This is a MIME encoded message.".$eol.$eol;
    

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 2017-10-04
      • 2013-07-13
      • 1970-01-01
      • 2016-11-30
      • 2015-06-19
      • 2010-11-26
      • 2016-12-17
      相关资源
      最近更新 更多