-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。
您應該使用搜索功能: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 –
我先搜索了一下。我遇到的所有問題似乎都是關於用戶提供的文件或某個位置的文件。似乎沒有討論通過php動態創建的文件。 – dcp3450
無論是提供的文件還是由php生成的文件都無關緊要,因爲在大多數情況下,當附加到郵件消息時,需要處理它們。 –