-1

我使用 Dreamweaver/HTML 创建了一个表单,并将它与一个通过电子邮件发送表单的 PHP 脚本连接起来。我对 PHP 知之甚少,我正试图让联系表格有一个“图片上传”,然后在发送表格时将其附加到电子邮件中。我测试了表格,它工作得很好。只有上传不做任何事情。我希望有人可以帮助我解决这个问题。这是我的html表单。

<form action="quote_form.php" method="post" enctype="multipart/form-data" name="QuoteForm" id="QuoteForm">
  <p>
    <label for="name">Full Name<span style="color: #FF0000">*</span></label>
    <input name="name" type="text" id="name" size="40" maxlength="40" />
  </p>
  <p>
    <label for="phone">Phone #<span style="color: #FF0000">*</span></label>
    <input name="phone" type="text" id="phone" size="30" maxlength="30" />
  </p>
  <p>
    <label for="email">Email<span style="color: #FF0000">*</span></label>
    <input name="email" type="text" id="email" size="30" maxlength="30" />
  </p>
  <span style="font-style: italic">Please check at least one or both boxes<span style="color: #FF0000">*</span></span>
  <p>
    <input type="checkbox" name="servicetype[]" value="pawn" />
    Pawn
    <input type="checkbox" name="servicetype[]" value="buy" />
    Buy<br /><br />
  </p>
  <p><span style="color: #ff9600; font-size: 18px;">Tell Us About Your Item</span><br />
    <span style="font-style: italic">(the more information the better --- description i.e. brand, model and condition of item.)</span></p>
  <p>
    <textarea name="comments" id="comments" cols="50" rows="10"></textarea>
  </p>
  <p>
    <label for="file">Select File To Upload<span style="color: #FF0000">*</span></label>
    <input type="file" name="file" id="file" size="30" maxlength="30" />
  </p>
  <p>
    <input type="submit" name="submit" id="sumbit" value="Submit" />
    <input name="reset" type="reset" id="reset" value="Reset Form" />
  </p>
  <p>
    <input name="recipient" type="hidden" id="recipient" value="isabelpolanco23@gmail.com" />
    <input name="redirect" type="hidden" id="redirect" value="thankyou-pg.html" />
    <br />
  </p>
</form>

这是我要发送电子邮件的 php 脚本

<?php
if(isset($_POST['email'])) {
    // FORM DETAILS EMAIL
  $email_to = "myemail@gmail.com";
  $email_subject = "Pawn/Buy Quote Form Results";
    $email_message = "Quote form details below + attachment.\n\n";

  function died($error) {
    // ERROR CODES HERE
    echo "We are very 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();
  }

    // VALIDATION EXPECTED DATA EXIST 
  if(!isset($_POST['name']) ||
     !isset($_POST['email']) ||
     !isset($_POST['phone']) ||
     !isset($_POST['comments'])){
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
  }

  // invalid emailaddress
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

  }

  //FORM DETAILS     
  $name = $_POST['name']; // required
  $email_from = $_POST['email']; // required
  $phone = $_POST['phone']; // required
  $comments = $_POST['comments']; // required
  $servicetype = array("pawn" => false, "buy" => false);  //checkboxes

  foreach ($_POST["servicetype"] as $value) {
    if ($value=="pawn")
      $servicetype["pawn"] = true;
    else if ($value=="buy")
      $servicetype["buy"] = true;
  }

  //ERROR MESSAGES    
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  $string_exp = "/^[A-Za-z .'-]+$/";
    if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }

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

  //UPLOAD IMAGE
  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/gif")
       || ($_FILES["file"]["type"] == "image/jpeg")
       || ($_FILES["file"]["type"] == "image/jpg")
       || ($_FILES["file"]["type"] == "image/pjpeg")
       || ($_FILES["file"]["type"] == "image/x-png")
       || ($_FILES["file"]["type"] == "image/png"))
      && ($_FILES["file"]["size"] < 10000)
      && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

      if (file_exists("upload/" . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
      else {
        move_uploaded_file($_FILES["file"]["tmp_name"],
                           "upload/" . $_FILES["file"]["name"]);
        echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
  else {
    echo "Invalid file";
  }

  //EMAIL MESSAGE DETAILS     
  $email_message .= "Name: ".clean_string($name)."\n";
  $email_message .= "Email: ".clean_string($email_from)."\n";
  $email_message .= "Phone: ".clean_string($phone)."\n";
  $email_message .= "Pawn: " . clean_string(($servicetype["pawn"]) ? "Yes" : "No") . "\n";
  $email_message .= "Buy: " . clean_string(($servicetype["buy"]) ? "Yes" : "No") . "\n"; 
  $email_message .= "About My Item: ".clean_string($comments)."\n";

  //now Attach all files submitted
  $mail->AddAttachment("uploads"."/".$_FILES["file"]["name"]);

  //EMAIL HEADERS
  $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);  
?>

<?php
//REDIRECT PAGE
header("Location:thankyou-pg.html");
exit;
?>

<?php
}
?>
4

1 回答 1

0

您似乎主要是手动构建邮件数据以与mail()内置程序一起使用,但突然之间您有了以下行:

$mail->AddAttachment("uploads"."/".$_FILES["image"]["type"]);

除了 Fred 在评论中指出的错字($_FILES["image"]["type"]应该是$_FILES["file"]["name"])之外,您的代码似乎从未定义$mail或再次使用它。

看起来您已经开始查看邮件库(PHPMailer、SwiftMailer 或类似的),但实际上并未正确阅读说明。这可能是一个好方法 - 它比尝试滚动您自己的字符串替换例程更安全,并且一旦您进入附件等,更有可能以有效的电子邮件结束 - 但您需要转换所有代码使用该库,而不仅仅是调用它并希望它以某种方式与您现有的代码合并。

于 2013-09-15T23:39:53.587 回答