2011-11-20 90 views
-1

我有一个用户填写和提交的表单。一旦提交表单需要从输入中创建一个文档,使用mail()函数将该文档附加到一封电子邮件中,并将其发送给我指定的人员。如何将php创建的文档附加到电子邮件?

目前我创建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大小。这是我用来附加数据和发送电子邮件的脚本。它与我在用户提供文件时使用的脚本相同。我将脚本从读取文件中的数据直接转换为“body”。

$headers = "From: [email protected]"; 

    // 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('[email protected]', 'test file', $message, $headers); 
    if ($ok) 
    { 
     echo 'yes'; 
    } else 
    { 
     echo 'no'; 
    } 

我不明白为什么这个文件只有3k。

+0

您应该使用搜索功能:http://stackoverflow.com/questions/565082/attach-file-in-formmail-php-formmail http://stackoverflow.com/questions/1330626/how-can-i -send-email-with-attachments-from-a-php-form http://stackoverflow.com/questions/1578389/attach-file-to-email-using-php http://stackoverflow.com/questions/3582796/email-attachment-in-php http://stackoverflow.com/questions/5480383/php-create-file-from-string-and-attach-to-email –

+0

我先搜索了一下。我遇到的所有问题似乎都是关于用户提供的文件或某个位置的文件。似乎没有讨论通过php动态创建的文件。 – dcp3450

+0

无论是提供的文件还是由php生成的文件都无关紧要,因为在大多数情况下,当附加到邮件消息时,需要处理它们。 –

回答

0

作为编辑添加的脚本是功能脚本。空白文件在我的最后(由于睡眠不足而导致用户错误)是一个问题。对于那些遇到这个问题的人:编辑后的脚本是一个工作脚本。

相关问题