php - 如何使用 mail() 或 PHPMailer 创建带有文件图像上传的 HTML5 电子邮件联系表单?
问题描述
我已经为一个课程项目(不是 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="jdoe@example.com" 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 = "myemail@gmail.com";
$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!";
}
我希望它能够通过,但是我不确定它为什么会输出错误以及我在哪里搞砸了。
解决方案
也许是因为您希望提交帖子数据?
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"/>
推荐阅读
- vba - Vba 在一个过程中有两个不同的错误处理程序
- c# - 是否可以使用值复制语义实现 ac# 类?
- python - 关于在 Python 中进行并行触发和忘记函数调用的任何建议?
- javascript - 如何链接 Intl.Collator().compare?
- itemtemplate - RadListView OnSelectedIndexChanged ...如何获取所选项目的选定索引
- html - 引导背景图像出现在我网站的每个页面上
- angular - 如何删除primeng条形图条上的数字标签?
- masstransit - 如何使用 MassTransit 将消费者配置为外部队列(另一个应用程序)
- c - 尝试在 C 上运行 hello world 并在 VS 上的 Mac 上抛出此错误:zsh: parse error near `&&'
- amazon-web-services - 无法通过 lamda 跨 aws 账户进行 Elastic Search 查询