【发布时间】:2012-01-02 06:42:50
【问题描述】:
我有一个用户填写并提交的表单。提交后,表单需要根据输入创建一个文档,使用 mail() 函数将文档附加到电子邮件中,然后通过电子邮件将其发送给我指定的人。
目前我正在以这种方式创建 doc 文件:
HEADER("Content-Type: application/vnd.ms-word; name='word'");
HEADER("Content-type: application/octet-stream");
HEADER("Content-Disposition: attachment; filename=filename_here.doc");
HEADER("Cache-Control: must-revalidate, post-check=0, pre-check=0");
HEADER("Pragma: no-cache");
HEADER("Expires: 0");
ECHO $header."\n".$body;
EXIT;
显然这会输出文档。我想将其附加到电子邮件中,因为我不希望用户获取文档。根据情况,用户将被重定向到确认或错误页面。
谢谢!
------------- 编辑 -----
我现在有发送带有附件的电子邮件的脚本。但是,附件只有 3k 大小。这是我用来附加数据和发送电子邮件的脚本。它与我在用户提供文件时使用的脚本相同。我将脚本从读取文件中的数据更改为直接对“正文”进行基本编码。
$headers = "From: fromperson@email.com";
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$content . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($body));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: application/ms-word;\n" .
" name=\"testfile.doc\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"testfile.doc\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// Send the message
$ok = @mail('toperson@email.com', 'test file', $message, $headers);
if ($ok)
{
echo 'yes';
} else
{
echo 'no';
}
我不明白为什么文件只有 3k。
【问题讨论】:
-
我先搜索了一下。我遇到的所有问题似乎都是关于用户提供的文件或来自某个位置的文件。似乎没有讨论通过 php 动态创建的文件。
-
无论是提供的文件还是由 php 生成的文件都无关紧要,因为在大多数情况下,附加到邮件消息时需要进行相同的处理。
-
好的。我认为我当时遇到的问题是,我如何捕获动态创建的文件,因为它没有保存在任何地方?我有上述经验:输出文件。以及发送用户提供的文件但不创建文件然后通过电子邮件发送。以上将始终输出。
-
你已经在
$body中拥有了文件的二进制数据。
标签: php dynamic document attachment