如何使用mail()或PHPMailer创建带有文件图像上载的HTML5电子邮件联系表单?

问题描述 投票:2回答:2

我已经为一个课程项目(不是CS课程)部署了一个网站,并且正在制作一个联系表格,要求用户用照片提供他们的基本用户信息。但是,在表单提交时,我收到一条错误,该错误由我的if语句回显。

我正在使用以下教程:https://www.youtube.com/watch?v=zYocypr0Xig。我已经根据https://www.w3schools.com/php/php_file_upload.asp在php文件所在的目录中创建了一个名为“tmp_name”的目录。我已经浏览了超过10个小时,无法找到解决方案或更简洁的示例来指示原因,就像Youtube视频那样。

HTML:

<form action="betacontactform.php" method="post" enctype="multipart/form-data">
  <div class="form-container">
    <div class="columns" style="width: 500px; ">
      <h3 id="intro-box-headers">User Details:</h3>
      * First Name: <input type="text" name="firstName" placeholder="Jane" required>
      Last Name: <input type="text" name="lastName" placeholder="Doe">
      * Email: <input type="text" name="email" placeholder="[email protected]" required>
      * County: <input type="text" name="county" placeholder="County" required>
      * State: <input type="text" name="state" placeholder="State" required>
    </div>
      <span>* At least 1 picture of your project must be uploaded:</span>
      <input type="file" name="attachment" placeholder="Upload Images (required):" required/>
<!-- TODO: multiple optional image file uploads -->
      <br>
    </div>
  </div>
  <div style="width: 100%; text-align: center;">
    <input style="margin-left: 0px;" class="submit-form-button" type="submit" name="submit" value="Submit" />
  </div>
</form>

PHP:

<?php

if (isset($_POST['submit']) && isset($_FILES['attachment'])) {
  // $message contents
  $firstName = $_POST['firstName'];
  $lastName = $_POST['lastName'];
  $county = $_POST['county'];
  $state = $_POST['state'];

  // image upload attachment
  // store some variables
  $file_name = $_FILES['attachment']['name'];
  $temp_name = $_FILES['attachment']['tmp_name'];
  $file_type = $_FILES['attachment']['type'];

  // get the extension of the get_included_files
  $base = basename($file_name);
  $extension = substr($base, strlen($base)-4, strlen($base));

  // only these file types will be allowed
  $allowed_extensions = array(".png","jpeg",".jpg");

  // mail essentials
  $from = $_POST['email'];
  $to = "[email protected]";
  $subject = "New Client Alert!!";
  $message = "You have received an email from $firstName $lastName \n Location: $county $state";

  // things you need
  $file = $temp_name;
  $content = chunk_split(base64_encode(file_get_contents($file)));
  $uid = md5(uniqueid(time()));

  // standard email headers
  $header = "From: ".$from."\r\n";
  $header .= "Reply-To".$replyto."\r\n";
  $header .= "MIME-Version: 1.0\r\n";

  // declaring we have multiple kinds of email (i.e. plain text and attachment)
  $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  $header .= "This is a multi-part message in MIME format.\r\n";

  // plain text part
  $header .= "--".$uid."\r\n";
  $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  $header .= $message."\r\n\r\n";

  // file attachment part
  $header .= "--".$uid."\r\n";
  $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
  $header .= "Content-Transfer-Encoding: base64\r\n";
  $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
  $header .= $content."\r\n\r\n";

  mail($to, $subject, "", $header);
  header("Location: /website/postformpage.html");
} else {
  echo "Error!";
}

我希望它能够通过,但我不确定为什么它输出错误和我搞砸了。

php html html5 email
2个回答
1
投票

也许是因为你期望提交帖子数据?

isset($ _ POST ['submit'])?

取下它或只是更改按钮输入

<button style="margin-left: ;" class="submit-form-button" type="submit" name="submit">Submit!</button>

to

<input style="margin-left:0;" class="submit-form-button" type="submit" name="submit" value="Submit"/>

0
投票

也许是因为你期望提交帖子数据

isset($ _ POST [ '提交'])

取下它或只是更改按钮输入

<button style="margin-left: ;" class="submit-form-button" type="submit" name="submit">Submit!</button>

to

<input style="margin-left:0;" class="submit-form-button" type="submit" name="submit" value="Submit"/>
© www.soinside.com 2019 - 2024. All rights reserved.