2013-02-28 114 views
1

我想從PHP表單獲得兩個簡歷到我的電子郵件ID。我完美地獲得了所有信息,但附件已經空白。我找不到原因。PHP表單郵件發送空白附件

任何幫助將不勝感激。請在下面找到HTML和PHP代碼。提前致謝。

< ------------------------ HTML代碼------------------- --------------------->

<form name="Form5" method="post" action="js/curent-job.php" onsubmit="return ValidateForm2()"> 
<table width="55%" align="left" cellpadding="0" cellspacing="0"> 
<tr> 
<td colspan="3" style="height:70px;"> 

<p>Please submit your updated resume with the below details. </p> 

</td> 
</tr> 

<tr> 
<td width="93"><label for="Full_Name" class="required">Name:</label></td> 
<td width="40">&nbsp;</td> 
<td width="252"><input name="Full_Name" id="Full_Name" maxlength="80" style="width: 250px; height:20px" type="text"></td> 
</tr> 

<tr style="height:5px"> 
<td colspan="3"></td> 
</tr> 

<tr> 
<td><label for="Tel_No" class="required">Telephone:</label></td> 
<td>&nbsp;</td> 
<td><input name="Tel_No" id="Tel_No" maxlength="100" style="width: 250px; height:20px" type="text"></td> 
</tr> 

<tr style="height:5px"> 
<td colspan="3"></td> 
</tr> 

<tr> 
<td> <label for="Email_Address" class="required">Email Id:</label></td> 
<td>&nbsp;</td> 
<td> <input name="Email_Address" id="Email_Address" maxlength="100" style="width: 250px; height:20px" type="text"></td> 
</tr> 

<tr style="height:5px"> 
<td colspan="3"></td> 
</tr> 

<tr> 
<td> <label for="Resume" class="required">Resume 1:</label></td> 
<td>&nbsp;</td> 
<td> <input name="Resume" id="Resume" maxlength="100" type="file"></td> 
</tr> 


<tr> 
<td> <label for="Resume2" class="required">Resume 2 :</label></td> 
<td>&nbsp;</td> 
<td> <input name="Resume2" id="Resume2" maxlength="100" type="file"></td> 
</tr> 

<tr style="height:5px"> 
<td colspan="3"></td> 
</tr> 

<tr> 
<td><label for="Description" class="required">Description:</label></td> 
<td>&nbsp;</td> 
<td><textarea style="width: 250px; height: 60px;" name="Description" id="Description" maxlength="3000"></textarea></td> 
</tr> 

<tr style="height:5px"> 
<td colspan="3"></td> 
</tr> 

<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td><input type="image" src="images/button.png" width="75" height="30" /></td> 
</tr> 

</table> 
</form> 


<------------------------PHP CODE----------------------------------------> 


<?php 

if(isset($_POST['Email_Address'])) { 


    $files = array("Resume.docx", "Resume2.docx"); 

    $email_to = "[email protected]"; 
    $email_subject = "My Company - Career Enquiry"; 
    $thankyou = "http://www.mydomain.com/thanks.html"; 
    $email_sender = "[email protected]"; 

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 



    function died($error) { 
     echo "Sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 


    $full_name = $_POST['Full_Name']; // required 
    $email_from = $_POST['Email_Address']; // required 
    $telephone = $_POST['Tel_No']; //required 
    $comments = $_POST['Description']; // required 


    $email_message = "Message details below.\r\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Full Name: ".clean_string($full_name)."\r\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\r\n"; 
    $email_message .= "Contact Number: ".clean_string($telephone)."\r\n"; 
    $email_message .= "Message: ".clean_string($comments)."\r\n"; 


    // multipart boundary 
$email_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" . $email_message . "\n\n"; 
$email_message .= "--{$mime_boundary}\n"; 

// preparing attachments 
for($x=0;$x<count($files);$x++){ 
    $file = fopen($files[$x],"rb"); 
    $data = fread($file,filesize($files[$x])); 
    fclose($file); 
    $data = chunk_split(base64_encode($data)); 
    $email_message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
    $email_message .= "--{$mime_boundary}\n"; 
} 

$headers = 'From: '.$email_sender."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
// headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
@mail($email_to, $email_subject, $email_message, $headers); 
header("Location: $thankyou"); 
?> 
<script>location.replace('<?php echo $thankyou;?>')</script> 
<?php 
} 
die(); 
?> 

回答

1

enctype="multipart/form-data"在開幕形式標記是必要的,這使得形式傳送文件到服務器。

+0

感謝您的回覆。我在表格中添加了這個標籤。但仍然存在相同的問題 – user1269100 2013-02-28 07:17:11

0

它發送電子郵件怎麼一回事,因爲你所描述的文件不添加人工

$files = array("Resume.docx", "Resume2.docx"); 

的multipart/form-data的

enctype="multipart/form" 

和處理,如果上傳上傳的文件它會正常工作

+0

感謝您的朋友您的回覆。我在表格中添加了這個標籤。但仍然存在相同的問題.. :-( – user1269100 2013-02-28 06:49:10

+0

刪除行$ files = array(「Resume.docx」,「Resume2.docx」); – Vineet1982 2013-02-28 06:59:47