我正在製作一個PHP表單,允許用戶上傳附件並將其發送到我的電子郵件。我一直在尋找相當長的時間來完成它。 最後,我找到了這個。 http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/。它工作正常。 但是,當我自己修改它(更改字段)時,某些內容不太好。完成上傳附件之前發送的PHP郵件
<?php
$location=$_POST['location'];
$name_ha=$_POST['name_ha'];
$name_person=$_POST['name_person'];
$email=$_POST['email'];
$date_sent=$_POST['date_sent'];
$date_completed=$_POST['date_completed'];
$date_received=$_POST['date_received'];
$to="[email protected]" . "$email";
$message="to";
//*** Uniqid Session ***//
$sid = md5(uniqid(time()));
$header = "";
$header .= "From: ".$_POST["name_ha"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$sid."\n";
$header .= "Content-type: text/html; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $message."\n\n";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$FilesName = $_FILES["fileAttach"]["name"];
$Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$header .= "--".$sid."\n";
$header .= "Content-Type: application/octet-eam; name=\"".$FilesName."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$FilesName."\"\n\n";
$header .= $Content."\n\n";
}
$flgSend = @mail($to,"A new file for you!",null,$header); // @ = No Show Error //
if ($flgSend)
{
echo "Mail sent.";
}
?>
我從shotdev.com下載的文件和我修改的文件位於同一臺服務器上,位於同一文件夾下。 但是,對於我修改的那個,如果文件大小大於1MB,則會在附件上傳之前發送電子郵件(大約45%的上傳過程)。我收到的電子郵件中沒有附件,也沒有發件人的信息($ name_ha)。 另一方面,對於在shotdev.com上下載的文件,電子郵件只會在完整上傳附件後發送,不管它有多大。
腳本是否有任何錯誤,或缺少某些東西,導致此類事件發生?你的時間和幫助非常感謝。
我需要測試這個,第一印象是你有一些頭文件的問題,我不喜歡這段代碼是如何完成的。 – Develoger
我會首先回顧你所改變的問題。 –
嚴重的是,如果你試圖使用附件,甚至不要嘗試使用PHP的內置'mail()'函數。你會花一輩子的時間努力使它正常工作。使用[phpMailer類](http://code.google.com/a/apache-extras.org/p/phpmailer/)會更好。另請參閱我的答案在這裏:http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354 – SDC