我研究了互聯網,但找不到我需要的:/通過電子郵件形式的文件
我有一個聯繫表格,(PHP)。我沒有任何數據庫。它是一個簡單的PHP電子郵件形式,但現在我需要使用文件附件:/我如何通過聯繫表格發送電子郵件文件?訪客將從他的電腦瀏覽任何文件並通過表單發送電子郵件。
欣賞!!
我研究了互聯網,但找不到我需要的:/通過電子郵件形式的文件
我有一個聯繫表格,(PHP)。我沒有任何數據庫。它是一個簡單的PHP電子郵件形式,但現在我需要使用文件附件:/我如何通過聯繫表格發送電子郵件文件?訪客將從他的電腦瀏覽任何文件並通過表單發送電子郵件。
欣賞!!
我已經看了評論中的鏈接,並清理了一些功能。有一件事我使用了一個參數和heredocs的關聯數組,因爲在示例中使用php標記和輸出緩衝並不完全乾淨(或者像PHP一樣乾淨)。
http://aramk.com/php/php-sending-an-email-attachment/
emailFile(array(
'to' => '[email protected]',
'from' => '[email protected]',
'subject' => 'Some Subject',
'message' => '<b>Hello!</b>',
'plain ' => 'Get a new email client!',
'file' => '/path/to/file'
));
您可以從$_FILES
沿着文件路徑傳遞到"file"
說法。
使用文件上傳腳本,就像您可以在互聯網上找到的那樣(例如http://www.w3schools.com/php/php_file_upload.asp)。然後你有一些臨時文件,我們稱之爲file_to_send
。然後,只需使用I.devries的評論(http://webcheatsheet.com/php/send_email_text_html_attachment.php)中提及的代碼將附件與您的郵件一起發送。
下面你可以從上面提到的網站上找到一些複製粘貼的代碼,但需要進行必要的調整。
HTML:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file_to_send">Filename:</label>
<input type="file" name="file_to_send" id="file_to_send"><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP:
<?php
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: [email protected]\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file_to_send']['tmp_name'])));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
在此解釋: http://www.webcheatsheet.com/php/send_email_text_html_attachment.php – 2009-10-22 18:08:54
感謝鏈接@Vatos。我其實需要找出如何通過表單(文件輸入)附加文件。鏈接中的示例看起來不像通過表單文件輸入。 – 2009-10-22 18:23:38
這是一樣的東西,只需要替換: chunk_split(base64_encode(file_get_contents('attachment.zip'))) chunk_split(base64_encode('file_input_name''['tmp_name']'))) – 2009-10-22 18:40:00