2012-03-30 184 views
0

我正在处理一个包含文件上传的表单邮件,但该文件根本没有显示在测试电子邮件中。我调整了并重新调整了但无济于事。有什么建议么?PHP文件上传问题

形式的HTML部分被如下所示

<div class="contactleft"> 


     <form action="former.php" method="post" enctype="multipart/form-data" id="former"> 

     <div class="textbox"><span id="sprytextfield1"> 
      <label for="fname"></label> 
      <input name="fname" type="text" class="inputer" id="fname" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield2"> 
      <label for="lname"></label> 
      <input name="lname" type="text" class="inputer" id="lname" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield3"> 
      <label for="email"></label> 
      <input name="email" type="text" class="inputer" id="email" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield4"> 
      <label for="file"></label> 
      <label for="file"></label> 
      <label for="fileField"></label> 
      <input type="file" name="fileField" id="fileField" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 


     <div class="textbox"> 
      <label for="sender"></label> 
      <input type="submit" name="sender" id="sender" value="click to send message" /> 
     </div><!-- End TextBox --> 

      </form> 

    </div> 

并且被如下所示

<? 
    $mailto = '[email protected]'; // insert the email address you want the form sent to 
    //$returnpage = 'thanks.php'; // insert the name of the page/location you want the user to be returned to// 
    $sitename = '[siteripe.com]'; // insert the site name here, it will appear in the subject of your email 

/* Do not edit below this line unless you know what you're doing */ 

    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $email = $_POST['email'] ; 
    $file = $_POST['file']; 
    $subject = $_POST['subject']; 


    if (!eregi("^[a-z0-9]+([-_\.]?[a-z0-9])[email protected][a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){ 
    print("<strong>Error:</strong> this email address is not in a valid format.<br/><br/><a href='javascript:history.go(-1)'>Back</a>"); 
     exit; 
    } 

    $message = "\n$name submitted the following message:\n\n$message\n\n$name's contact details are as follows:\n\nFirst Name: $fname\nLast Name: $lname\nEmail Address: $email\nForm: $file"; 

    mail($mailto, "$subject", $message, "From: $email"); 

?> 
+0

邮件函数返回成功的尝试是$ var =邮件真..和看到的响应 – 2012-03-30 00:42:55

+0

嘛,$ _ POST [“文件”]总是会失败,因为你的文件上传输入被命名为'fileField'。尝试$ _POST ['fileField'],如果你在文件名后面。 – 2012-03-30 00:44:08

回答

1

您不能仅仅将$ file作为$ message的一部分来传递,而无需添加一些额外的标头,表明该电子邮件是“多部分”电子邮件,其中$ file作为附件。

如果您想使用vanilla PHP,请查看docs以了解mail()函数,这里有一些注释可以告诉您如何执行您所要求的操作。

如果你愿意看第三方库,我会建议Zend Framework,他们有一个Zend_Mail_Attachment类,它提供了一个干净的界面,用于发送带有附件的电子邮件。

0

邮寄者尝试改变:

$file = $_POST['file']; 

到:

$file = $_POST['fileField']; 
0

这应该工作:

<html> 
    <head> 
     <title>Test</title> 
    </head> 
    <body> 
<? 
if(count($_POST) > 0){ 
    $mailto = '[email protected]'; // insert the email address you want the form sent to 
    //$returnpage = 'thanks.php'; // insert the name of the page/location you want the user to be returned to// 
    $sitename = '[siteripe.com]'; // insert the site name here, it will appear in the subject of your email 

/* Do not edit below this line unless you know what you're doing */ 

    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $name = $lname . ', ' . $fname; 
    $email = $_POST['email'] ; 
    $subject = (array_key_exists('subject', $_POST))?$_POST['subject']:'Default subject'; 


    if ([email protected]("^[a-z0-9]+([-_\.]?[a-z0-9])[email protected][a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){ 
    print("<strong>Error:</strong> this email address is not in a valid format.<br/><br/><a href='javascript:history.go(-1)'>Back</a>"); 
     exit; 
    } 

    $message = "\n$name submitted the following message:\n\n...\n\n$name's contact details are as follows:\n\nFirst Name: $fname\nLast Name: $lname\nEmail Address: $email"; 


    $rand = md5(time()); 
    $mime_boundary = '==Multipart_Boundary_x' . $rand . 'x'; 

    if(array_key_exists('fileField', $_FILES)){ 
     if(is_file($_FILES['fileField']['tmp_name'])){ 
      $message .= "--{$mime_boundary}\n"; 
      $fp = @fopen($_FILES['fileField']['tmp_name'],"rb"); 
     $data = @fread($fp,filesize($_FILES['fileField']['tmp_name'])); 
        @fclose($fp); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "Content-Type: application/octet-stream; name=\"".$_FILES['fileField']['tmp_name']."\"\n" . 
      "Content-Description: ".$_FILES['fileField']['name']."\n" . 
      "Content-Disposition: attachment;\n" . " filename=\"".$_FILES['fileField']['name']."\"; size=".filesize($_FILES['fileField']['tmp_name']).";\n" . 
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      } 
     } 
    $message .= "--{$mime_boundary}--"; 

    $headers = 'From: ' . $email; 
    mail($mailto, "$subject", $message, $headers); 
} 
?> 
<div class="contactleft"> 
     <form action="" method="post" enctype="multipart/form-data" id="former"> 
     <div class="textbox"><span id="sprytextfield1"> 
      <label for="fname"></label> 
      <input name="fname" type="text" class="inputer" id="fname" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield2"> 
      <label for="lname"></label> 
      <input name="lname" type="text" class="inputer" id="lname" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield3"> 
      <label for="email"></label> 
      <input name="email" type="text" class="inputer" id="email" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"><span id="sprytextfield4"> 
      <label for="file"></label> 
      <label for="file"></label> 
      <label for="fileField"></label> 
      <input type="file" name="fileField" id="fileField" /> 
      <span class="textfieldRequiredMsg">A value is required.</span></span></div><!-- End TectBox --> 
     <div class="textbox"> 
      <label for="sender"></label> 
      <input type="submit" name="sender" id="sender" value="click to send message" /> 
     </div><!-- End TextBox --> 
      </form> 
    </div> 
    </body> 
</html> 
+0

您的代码发生了一些更改,因为有几个E_NOTICE错误... – 2012-03-30 01:01:31

0

我还有我看到你做了mail()函数调用,但$文件不在它。我个人使用phpmailer类,然后声明一个邮件对象,然后向它添加组件。

// SEND THANK YOU EMAIL 
include('class.phpmailer.php'); 
$mail = new PHPMailer(); 
// Sender is the Reply-Path address; seems important 
$mail->Sender = "[email protected]"; 
$mail->From  = "[email protected]"; 
$mail->FromName = "Ca Cycleworks automated e-mail"; 
// $mail->AddAddress() is the To: 
$blahtext=stripslashes($_SESSION['address_info']['b_first_name'])." ".stripslashes($_SESSION['address_info']['b_last_name']); 
$mail->AddAddress($_SESSION['address_info']['b_email'],$blahtext); 
$mail->AddReplyTo("[email protected]", "Ca Cycleworks Orders"); 
$subject = "Your Ca-Cycleworks.com Order # ".$_SESSION['order_number']." confirmation"; 
$mail->Subject = $subject; 
$mail->AltBody = $text_body; 
// ISO 8859-1 summary: http://www.btinternet.com/~andrew.murphy/html_character_set.html 
// 
// $Encoding 
//  PHPMailer::$Encoding in class.phpmailer.php 
//  Sets the Encoding of the message. Options for this are "8bit", "7bit", "binary", "base64", and "quoted-printable". 
// string $ContentType = "text/plain" (line 42) 
// False Sets ContentType = "text/html" or "text/plain" 
//$mail->IsHTML(false); 
$mail->Body = $body; 
$mail->IsSMTP(); 
$mail->SMTPAuth = true; 
$mail->Username = "[email protected]"; 
$mail->Password = "xxxxxxxxxxxx";