提交获取空白页面后的PHP附件表单

时间:2016-08-18 09:38:45

标签: php forms attachment

提交表格后我收到邮件,但是它会显示白色黑屏,并显示留言信息。

我尝试在提交表单后重新加载页面,但收到错误。

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {

  $to = "name@gmail.com";
  $subject = "E-mail with attachment";
  $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

  // generate a random string to be used as the boundary marker
  $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

  // now we'll build the message headers
  $headers = "From: $from\r\n" .
      "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

  $message = "Canditade Resume";
  // when we use it
  $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
      $message . "\n\n";

  // iterating each File type
  print_r($_FILES);

  foreach ($_FILES as $userfile) {

    $tmp_name = $userfile['tmp_name'];
    $type = $userfile['type'];
    $name = $userfile['name'];
    $size = $userfile['size'];


    if (file_exists($tmp_name)) {
      if (is_uploaded_file($tmp_name)) {
        $file = fopen($tmp_name, 'rb');
        $data = fread($file, filesize($tmp_name));
        fclose($file);
        $data = chunk_split(base64_encode($data));
      }
      $message .= "--{$mime_boundary}\n" .
          "Content-Type: {$type};\n" .
          " name=\"{$name}\"\n" .
          "Content-Disposition: attachment;\n" .
          " filename=\"{$tmp_name}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data . "\n\n";
    }
  }
  // here's our closing mime boundary that indicates the last of the message
  $message.="--{$mime_boundary}--\n";
  // now we just send the message
  if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {
  ?>
  <form action="index.php" method="post"    enctype="multipart/form-data" name="form1">
      <label>Name</label>
      <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
      <label>Email Address</label>
      <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
      <label>Designation</label>
      <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
      <label>Upload Your CV</label>
      <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
      </div>
      <input type="submit" name="Submit" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
  </form>
<?php } ?>

请帮帮我

3 个答案:

答案 0 :(得分:0)

header location中提供要重定向的网页名称。

更改

if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {

if (@mail($to, $subject, $message, $headers))
    header("location:yourpage.php?message=Message Sent");
  else
    header("location:yourpage.php?message=Failed to send");
} else {

<强> yourpage.php

<?php
if(isset($_GET['message'])){
  echo $_GET['message'];
}
.
.

?>

答案 1 :(得分:0)

if (@mail($to, $subject, $message, $headers))
    //echo "Message Sent";
    header('location:email-success.php');
  else
    //echo "Failed to send";
    header('location:email-error.php');
}

答案 2 :(得分:0)

你的编码有点混乱。这是我做的调整:

我还注释掉了print_r($ _ FILES),因为这将显示当用户按下提交时您不希望显示的内容的数组。

所以我重命名了$ _POST并将其设为isset:

<?php
if (isset($_POST['send'])) {

    $to = "name@gmail.com";
    $subject = "E-mail with attachment";
    $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

    // generate a random string to be used as the boundary marker
    $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

    // now we'll build the message headers
    $headers = "From: $from\r\n" .
        "MIME-Version: 1.0\r\n" .
        "Content-Type: multipart/mixed;\r\n" .
        " boundary=\"{$mime_boundary}\"";

    $message = "Canditade Resume";
    // when we use it
    $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $message . "\n\n";

    // iterating each File type
    //print_r($_FILES);

    foreach ($_FILES as $userfile) {

        $tmp_name = $userfile['tmp_name'];
        $type = $userfile['type'];
        $name = $userfile['name'];
        $size = $userfile['size'];


        if (file_exists($tmp_name)) {
            if (is_uploaded_file($tmp_name)) {
                $file = fopen($tmp_name, 'rb');
                $data = fread($file, filesize($tmp_name));
                fclose($file);
                $data = chunk_split(base64_encode($data));
            }
            $message .= "--{$mime_boundary}\n" .
                "Content-Type: {$type};\n" .
                " name=\"{$name}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$tmp_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n";
        }
    }
    // here's our closing mime boundary that indicates the last of the message
    $message .= "--{$mime_boundary}--\n";
    // now we just send the message
    if (@mail($to, $subject, $message, $headers)) {
        echo "Message Sent";
    } else {
        echo "Failed to send";
    }
}
    ?>

不确定为什么要将PHP包装在html端,但这就是为什么你没有被带回到表单端而只是一个空白页。

 <form action="" method="post"    enctype="multipart/form-data" name="form1">
        <label>Name</label>
        <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
        <label>Email Address</label>
        <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
        <label>Designation</label>
        <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
        <label>Upload Your CV</label>
        <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
        </div>
        <input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
    </form>

我已经测试了这段代码,然后重定向回到表单上方的“已发送消息”(正如您在任何联系表单上看到的那样)。

相关问题