php - 无法从我的网站接收来自申请表的邮件
问题描述
我在我的网站上添加了一份培训申请表。使用模态框。这是代码
<button type="submit" onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Apply Now</button>
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<form method="POST" enctype="multipart/form-data" class="modal-content" action="sendemailcontact.php">
<div class="container">
<h1>Application Form</h1>
<p>Please fill in this form to apply for the training.</p>
<hr>
<label for="name"><b>Name</b></label><br>
<input type="text" placeholder="Enter name" name="name" id="name" required><br>
<label for="email"><b>Email</b></label><br>
<input type="text" placeholder="Enter Email" name="email" id="email" required><br>
<label for="phone"><b>Phone number</b></label><br>
<input type="text" placeholder="Enter phone number" name="phone" id="phone" required><br>
<label for="location"><b>Location</b></label><br>
<input type="text" placeholder="Enter location" name="location" id="location" required><br>
<label for="college"><b>College</b></label><br>
<input type="text" placeholder="Enter college" name="college" id="college" required><br>
<!--<label for="uploaded_file"><b>Upload resume</b></label>
<input class="upl" type="file" name="uploaded_file" id="uploaded_file" required>-->
<div class="clearfix">
<input style="width:30%;" type="submit" name="apply" id="apply" class="signupbtn" value="Apply">
</div>
</div>
</form>
</div>
这个表格到我的 php 代码,将邮件发送到我的电子邮件地址,从上面的表格中收到每个申请人的详细信息。这是我的php代码
<?php
//error_reporting(-1);
//ini_set('display_errors', 'On');
//set_error_handler("var_dump");
$errors = '';
$myemail = 'example@gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['location']) ||
empty($_POST['college']))
{
$errors .= "\n Error: all fields are required";
}
$email_address = $_POST['email'];
$name = $_POST['name'];
$phno = $_POST['phone'];
$location = $_POST['location'];
$college = $_POST['college'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Career Oriented Training Form submission: $name";
$email_body = "You have received a new applicant. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phno \n Location: $location \n College: $college";
$headers = "From: $email_address\n";
$headers .= "Reply-To: $myemail";
mail($to,$email_subject,$email_body,$headers);
header('Location:index.html');
}
?>
当我点击提交时,它会进入一个空白页面。我也收不到任何邮件。请帮我
解决方案
您需要让它回显 $errors ,因为这会告诉您它失败的原因。您正在存储 $errors 并忽略它,而不是检查它是否为空。
也不要使用正则表达式来验证电子邮件地址。具体见http://php.net/manual/en/filter.filters.validate.phpFILTER_VALIDATE_EMAIL
推荐阅读
- javascript - 将 JSON 数组输出到行中的 Google Apps 脚本
- git - 如何签出远程分支并与其他团队成员协作处理它?
- sql-server - 在将新表添加回历史表之前,在历史表中的新表上设置值
- html - Cognos 11 本地分析 - 运行报告时保存生成的 html 代码
- java - 套接字实例不会连接到本地服务器
- python - 如何直接在 Ubuntu 终端中运行 Python 模块?
- angular - 解析 http://localhost:8080/home 期间的 Http 失败
- python - Anaconda 不会更新:“无默认基础环境”错误
- php - 如何在 VSCode Snippet 的制表位内使用 Emmet 快捷方式?
- java - 向用户显示 pdf 文件夹及其内容