PHP脚本通过电子邮件发送带有附件的联系表单


php script to email contact form with attachments

我使用 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
}
?>

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

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

除了Fred在注释中指出的错别字($_FILES["image"]["type"]应该$_FILES["file"]["name"]),你的代码似乎从来没有定义过$mail,或者再次使用它。

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