【问题标题】:PHP how to send a pdf attachment without it being corruptedPHP如何在不损坏的情况下发送pdf附件
【发布时间】:2016-03-23 18:51:38
【问题描述】:

我在发送 pdf 文件的简单功能时遇到了一些问题 - 电子邮件发送但附件在我尝试打开它时已损坏,所以显然我的功能做错了。

任何帮助将不胜感激。

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {

$file = $path.$filename;

$file_size = filesize($file);

$handle = fopen($file, "r");

$content = fread($handle, $file_size);

fclose($handle);

$content = chunk_split(base64_encode($content));

$uid = md5(uniqid(time()));

$header = "From: ".$from_name." <".$from_mail.">\r\n";

$header .= "Reply-To: ".$replyto."\r\n";

$header .= "MIME-Version: 1.0\r\n";

$header .= "This is a multi-part message in MIME format.\r\n";

$header .= "--".$uid."\r\n";

$header .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n";

$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

$header .= $message."\r\n\r\n";

$header .= "--".$uid."\r\n";

$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 

$header .= "Content-Transfer-Encoding: base64\r\n";

$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";

$header .= $content."\r\n\r\n";

$header .= "--".$uid."--";






wp_mail($mailto, $subject, $message, $header);


}

【问题讨论】:

  • 不要建立自己的 mime 电子邮件。使用适当的邮件包,如 phpmailer 和 swiftmailer。这会将几乎所有代码减少为单个 $mail-&gt;AddAttachment(...)-type 调用。既然你已经用 phpmailer 标记了这个问题 - 你为什么不使用它?

标签: php pdf phpmailer


【解决方案1】:

您的电子邮件标题偏离了很多。一个适当的多部分消息看起来像这样:

Content-Transfer-Encoding: binary
Content-Type: multipart/mixed; boundary="_----------=_1458761739257530"
MIME-Version: 1.0
Date: xxxxx
From: xxxxx
To: xxxxx
Reply-To: xxxx
Subject: xxxx

This is a multi-part message in MIME format.

--_----------=_1458761739257530
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
Content-Type: text/plain

This is the plain-text part of my message
That's all.

--_----------=_1458761739257530
Content-Disposition: attachment; filename="myfile.pdf"
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name="myfile.pdf"

<base64 content here>

--_----------=_1458761739257530--

但实际上,您不应该自己生成这样的标题 - 有很多方法可以搞砸它。相反,请使用任意数量的现有 PHP 库来生成 MIME 标头、进行编码并为您处理邮件。

例如,您的发行版很可能包含 PEAR 模块“Mail”和“Mail_Mime”,或者您可以使用 pear install Mail Mail_Mime 轻松安装它们

然后执行以下操作:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {

    require_once('Mail.php');
    require_once('Mail/mime.php');

    $file = $path.$filename;

    $headers = array (
      'From'     => $from_mail,
      'To'       => $mailto,
      'Reply-To' => $replyto,
      'Subject'  => $subject,
    );

    $mime = new Mail_mime(array('eol' => "\n"));

    $mime->SetTXTBody($message);
    $mime->addAttachment($file, 'application/octet-stream');

    $mime_body    = $mime->get();
    $mime_headers = $mime->headers($headers);

    $mail =& Mail::factory('mail');
    $mail->send($mailto, $mime_headers, $mime_body);

     if (PEAR::isError($mail)) {
      echo("<p>ERROR:" . $mail->getMessage() . "</p>\n");
    } else {
      echo("<p>Message successfully sent!</p>\n");
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2012-10-27
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多