【发布时间】:2019-01-30 01:37:25
【问题描述】:
我的 index.html 和 contact.html 页面上目前有一个联系表。在测试 PHP 联系表单时,他们没有给我发送电子邮件或发出任何消息已发送的通知,只是似乎什么也没做。
HTML
<div class="form">
<form action="php/contact.php" method="post">
<h5 class="grey-bg text-center iq-font-black iq-tw-6 iq-pall-20">Service Inquiry</h5>
<div class="iq-pall-30">
<div class="row">
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="name" placeholder="Name" id="name" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="email" placeholder="Email" id="email" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<select name="service" id="service">
<option value="Choose Service" >Choose Service</option>
<option value="test1" >test1</option>
<option value="test" >Test</option>
<span class="error">A service is required</span>
</select>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="phonenumber" placeholder="Phone Number" id="phonenumber" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field ">
<textarea class="form-control" placeholder="Comment" rows="5" id="comment" style="resize: none;"></textarea>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<button id="submit" name="submit" type="submit" value="Send" class="button pull-right iq-mt-20">Send Message</button>
</div>
</div>
</div>
</form>
</div>
PHP 代码
<?php $ToEmail = 'test@test.com';
$EmailSubject = 'User Contact Information';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Service: ".$_POST["service"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Service: ".$_POST["service"]."";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");?>
我收到的邮件是这样的:
User Contact Information
Name: Email: Phone: Comment:
这些字段是否与 PHP 表单和发送消息按钮匹配,如果电子邮件已发送,如何响应?
【问题讨论】:
-
您应该将问题的范围限制为一个单独的、离散的、可回答的问题。您对验证和 CAPTCHA 实施的询问更适合单独提出问题。
-
首先,您应该始终清理您的输入。其次,当您发送电子邮件失败时,您会收到错误或异常。如果你想验证,你可以用 Javascript 或 PHP 来做,网上有很多例子。 Captcha 很容易实现,网上也有很多例子
-
对您的 POST 数据执行
var_dump并查看它是否存在。 -
var_dump($_POST); var_dump($mailheader); var_dump($MESSAGE_BODY);
-
@LahiruTM 这似乎是用原子弹打兔子——除非这个脚本有任何额外的复杂性,我会阻止 OP 尝试实现像 PHPMailer 这样复杂的东西,因为它似乎内置了 @987654325 @ 函数可以很好地满足他们的用例。
标签: php html forms email contact-form