PHP表单错误 - HTML&图片上传

时间:2014-09-04 14:31:07

标签: php html forms email

所以有点快速的背景。我刚刚开始进入php,我已经在这几天了。抱歉,如果我的代码很乱,但我试图尽可能多地标记。

我有一个表格,在完成时,应该发生3件事。

  1. 电子邮件发送给收件人,所有填写的详细信息都通过html表+图像附件中的格式发送。
  2. 纯文本电子邮件将发送给表单申请人,谢谢。
  3. 此外,表格提交成功后,表格申请人即可 自动定向到“谢谢”页面。
  4. •如果上传/附加了填写AND图像的所有必填字段,则一切正常。整个过程运行无误。

    •如果没有上传/附加图像,我会收到以下消息

      

    警告:file():第49行的/home/website/public_html/repairs/contact.php中的文件名不能为空

         

    警告:无法修改标题信息 - 已在第108行的/home/website/public_html/repairs/contact.php中发送的标题(由/home/website/public_html/repairs/contact.php:49开始输出)< / p>

    接着是收到的电子邮件,其中包含所有html标签/代码/纯文本。

    仍然收到发送给“表格申请人”的明文电子邮件,谢谢。

    请参阅下面的php代码

    <?php
    
     // multiple recipients
    $to = $_REQUEST['sendto'] ; 
    $from = $_REQUEST['email'] ;
    
    // subject
    $subject = "Repairs Request Form"; 
    $fname = $_REQUEST['fname'] ; 
    
    $message = '
    <html>
    <body>
    <table rules="all" style="border-color: #666; border="1px" "cellpadding="10">
    <tr><td style="background: #eee;" colspan="2"><strong>Repairs Request Enquiry</strong></td></tr>
    <tr><td><strong>First Name:</strong> </td><td>' . strip_tags($_POST['fname']) . '</td></tr>
    <tr><td><strong>Last Name:</strong> </td><td>' . strip_tags($_POST['lname']) . '</td></tr>
    <tr><td><strong>Email:</strong> </td><td>' . strip_tags($_POST['email']) . '</td></tr>
    <tr><td><strong>Contact Number:</strong> </td><td>' . strip_tags($_POST['phone']) . '</td></tr>
    <tr><td><strong>Address:</strong> </td><td>' . strip_tags($_POST['address']) . '</td></tr>
    <tr><td><strong>Suburb:</strong> </td><td>' . strip_tags($_POST['suburb']) . '</td></tr>
    <tr><td><strong>Postcode:</strong> </td><td>' . strip_tags($_POST['postcode']) . '</td></tr>
    <tr><td><strong>Type of repair:</strong> </td><td>' . strip_tags($_POST['repair']) . '</td></tr>
    <tr><td><strong>Nature of the problem:</strong> </td><td>' . $_POST['problem'] . '</td></tr>
    <tr><td><strong>Is this urgent:</strong> </td><td>' . $_POST['urgent'] . '</td></tr>
    <tr><td><strong>Type of Urgency:</strong> </td><td>' . $_POST['urgent_type'] . '</td></tr>
    <tr><td><strong>Master key access required:</strong> </td><td>' . $_POST['masteraccess'] . '</td></tr>
    </table>
    </body>
    </html>'
    ;
    
     //ATTACHMENT START
    
    /* GET File Variables */ 
    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 
    
    /* Start of headers */ 
    $headers = "From: $from"; 
    
    //**XXXXXXXXXXXXXXXX  PROBLEM IS HERE XXXXXXXXXXXXXXXX**    
    
    if (file($tmpName)) { 
      /* Reading file ('rb' = read binary)  */
      $file = fopen($tmpName,'rb'); 
      $data = fread($file,filesize($tmpName)); 
      fclose($file); 
    
    
      /* a boundary string */
      $randomVal = md5(time()); 
      $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 
    
      /* Header for File Attachment */
      $headers .= "\nMIME-Version: 1.0\n"; 
      $headers .= "Content-Type: multipart/mixed;\n" ;
      $headers .= " boundary=\"{$mimeBoundary}\""; 
    
      /* Multipart Boundary above message */
      $message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mimeBoundary}\n" . 
      "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $message . "\n\n"; 
    
      /* Encoding file data */
      $data = chunk_split(base64_encode($data)); 
    
      /* Adding attchment-file to message*/
      $message .= "--{$mimeBoundary}\n" . 
      "Content-Type: {$fileType};\n" . 
      " name=\"{$fileName}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" . 
      "--{$mimeBoundary}--\n"; 
    } 
     //END OF ATTACHMENT
    
    
      //Message/email to the customer.
     $headers2 = "From: noreply@website.com.au"; 
     $subject2 = "Thank you. Your e-mail has been logged."; 
     $autoreply = "Thank you. Your e-mail has been logged.
    We will attend to your request and be in touch shortly.
    Rest assured, we are doing all we can to speed up this process.
    Regards,
    Repairs Team";
    
    
     if($from == '') {print "You have not entered an email, please go back and try again";} 
     else { 
     if($fname == '') {print "You have not entered your first name";} 
     else { 
     $send = mail($to, $subject, $message, $headers); 
     $send2 = mail($from, $subject2, $autoreply, $headers2); 
     if($send) 
     {header( "Location: http://www.website.com.au/thankyoupage/thankyou.html" );} 
     else 
     {print "We encountered an error sending your mail, please notify webmaster@website.com.au"; } 
     }
    }
    ?>
    

    我试着关注这篇文章,但我只是放弃它。 Php form upload and email

    非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

如果页面已经设置,则无法发送标题以重定向页面。

您可以尝试使用页面开头的ob_flush()来刷新对象。

答案 1 :(得分:0)

更改

if (file($tmpName)) {

if($_FILES["attachment"]["error"] != 0) {