PHP表单邮件发送空白附件

时间:2013-02-28 05:28:58

标签: php forms email email-attachments

我正在尝试从PHP表单中获取两份简历到我的电子邮件ID。我完全掌握了所有信息,但附件却空白了。我找不出原因。

任何帮助将不胜感激。请在下面找到HTML和PHP代码。提前谢谢。

< ------------------------ HTML CODE ------------------- --------------------->

<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 = "abc@mydomain.com";
    $email_subject = "My Company - Career Enquiry";
    $thankyou = "http://www.mydomain.com/thanks.html";
    $email_sender = "admin@mydomain.com";

    // 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();
?>

2 个答案:

答案 0 :(得分:1)

您的开始表单标记中需要

enctype="multipart/form-data",这允许表单将文件传输到服务器。

答案 1 :(得分:0)

由于您已手动描述文件,因此正在发送电子邮件

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

未添加multipart / form-data

enctype="multipart/form"

并处理上传的文件,如果上传它将正常工作