PHP联系人表单发送电子邮件错误

时间:2018-08-23 15:53:04

标签: php html forms email contact-form

我目前在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表单和“发送消息”按钮是否匹配,如果未发送电子邮件,如何响应?

1 个答案:

答案 0 :(得分:0)

几个错误:

  1. 您的<div class="iq-pall-30"><div class="row">元素未关闭,这会使您的标记无效,并可能影响您获取PHP脚本的输出。
  2. 您引用的表单元素实际上并不存在:
    1. $_POST["phone"]应该是对HTML表单元素<input type="text" name="phonenumber">的引用。您必须将变量引用更改为$_POST["phonenumber"]才能正确检索数据。
    2. $_POST["message"]可能是对textarea表单元素的引用,但是您尚未在标记中为该元素分配名称。您应该添加name="message"作为textarea的属性,以便正确检索该数据。