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

时间:2013-09-15 23:10:53

标签: php forms

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

1 个答案:

答案 0 :(得分:0)

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

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

除了Fred在评论中指出的拼写错误($_FILES["image"]["type"]应该是$_FILES["file"]["name"])之外,您的代码似乎没有定义$mail,或者再次使用它。< / p>

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