【发布时间】:2010-06-01 18:01:35
【问题描述】:
我想在 php 邮件中发送一个 pdf 文件。我知道文件的名称和位置(以 cup-pdf 格式打印的发票),并希望在我单击网站上的按钮时自动在 php 中发送。如何才能做到这一点?谢谢
【问题讨论】:
我想在 php 邮件中发送一个 pdf 文件。我知道文件的名称和位置(以 cup-pdf 格式打印的发票),并希望在我单击网站上的按钮时自动在 php 中发送。如何才能做到这一点?谢谢
【问题讨论】:
您应该使用为此构建的类之一,例如PEAR Mail。 更多解释,假设我们只使用 PDF 文件的纯文本 UTF8 内容。
uniqboundary 应该被某种独特的字符串替换$fileType 是您文件的 MIME 类型这里是代码:
$headers = 'MIME-Version: 1.0'."\r\n"
.'Content-Type: multipart/related; boundary="--uniqboundary"'."\r\n";
$body = '--uniqboundary."\r\n".
.'Content-Type: text/plain; charset=utf-8'."\r\n"
.'Content-Transfer-Encoding: 8bit'."\r\n\r\n"
.$text
.'--uniqboundary'."\r\n"
.'Content-Type: '.$fileType.'; name="'.basename($filename).'"'."\r\n"
.'Content-Transfer-Encoding: base64'."\r\n"
.'Content-Disposition: attachment; filename="'.basename($filename).'"'."\r\n\r\n";
$lineSize = filesize($filename) + 1;
$f = fopen($filename, 'r');
$chunks[] = chunk_split(base64_encode(fread($f, $lineSize)));
fclose($f);
$body .= implode("\r\n", $chunks)
.'--uniqboundary--'."\r\n";
mail($to, $subject, $body, $headers);
它应该可以工作。
【讨论】:
phpmailer 是一个不错的选择,但需要一个 smtp 帐户来制作它
【讨论】: