将文件附件添加到PHP表单以用于电子邮件

时间:2014-02-25 14:53:46

标签: php forms email

我知道有一些问题已经存在,但到目前为止,我所尝试过的任何内容都不适用于我的表单。目前,我有一个表单,其中包含三个文本输入,电子邮件地址,主题和消息。当用户点击发送时,它会通过电子邮件向我发送数据。电子邮件地址位于“来自”部分,主题位于“主题”部分,邮件位于电子邮件正文中。虽然,我如何添加一个向我发送用户附件的部分(任何尺寸都可以)?这是我目前的代码:

 <?php
function spamcheck($field)
  {
  // Sanitize e-mail address
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  // Validate e-mail address
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
?>
    </p>
    <p>
      <?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
  {
  ?>
    </p>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
      <p>Email address (make sure it's valid):</p>
    <p>
  <input type="text" name="from">
    </p>
    <p><br>
      Subject:</p>
    <p>
  <input type="text" name="subject">
    </p>
    <p><br>
      Message (copy your message before you send, just in case):      </p>
    <p>
      <textarea rows="10" cols="40" name="message"></textarea>
    </p>
    <p>
      <input class="sub" type="submit" name="submit" value="Send">
    </p>
    </form>
  <?php 
  }
else
  // the user has submitted the form
  {
  // Check if the "from" input field is filled out
  if (isset($_POST["from"]))
    {
    // Check if "from" email address is valid
    $mailcheck = spamcheck($_POST["from"]);
    if ($mailcheck==FALSE)
      {
      echo '<script type="text/javascript">alert("Please enter a valid email address!");
          window.location.href="../index";
      </script>';
      }
    else
      {
      $from = $_POST["from"]; // sender
      $subject = $_POST["subject"];
      $message = $_POST["message"];
      // message lines should not exceed 70 characters (PHP rule), so wrap it
      $message = wordwrap($message, 70);
      // send mail
      mail("byonexmusic@gmail.com",$subject,$message,"From: $from\n");
      echo "<font color=\"green\">Thank you for getting in touch!</font>";
      }
    }
  }
?>

非常感谢提前。 :)

0 个答案:

没有答案