【问题标题】:How do I attach a php created doc to an email?如何将 php 创建的文档附加到电子邮件中?
【发布时间】: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 dynamic document attachment


【解决方案1】:

作为编辑添加的脚本是功能性脚本。空白文件对我来说是个问题(用户因睡眠不足而出错)。对于那些遇到这个问题的人:编辑后的脚本是一个工作脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2018-09-07
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多