提交联系表格,我没有收到电子邮件

时间:2015-04-13 21:28:44

标签: php html forms

我在网站上添加了简单的联系表单,但在提交时我没有收到电子邮件。

我的HTML是:

<form action="contact.php" method="post" class="form-container">  
    <label for="name" class="form-title">Name</label> 
    <input id="name" class="form-field" type="text" name="cf_name" required>
    <br/>

    <label for="email" class="form-title">Email</label>
    <input id="email" class="form-field" type="email" name="cf_email" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$">
    <br/>

    <label for="message" class="form-title">Message</label>
    <textarea id="message" class="form-field" name="cf_message" required></textarea>
    <br/> 

    <div class="submit-container"> 
        <input class="submit-button" type="submit" value="Send">
    </div>
</form>

我的php是:

<?php   
     $field_name = $_POST['cf_name'];
     $field_email = $_POST['cf_email'];
     $field_message = $_POST['cf_message'];    

     $mail_to = 'info@littlerivermiami.com';
     $subject = 'Message From: '.$field_name;


     $body_message = 'E-mail: '.$field_email."\n";
     $body_message .= 'Message: '.$field_message;


     $headers = 'From: '.$field_email."\r\n";
     $headers .= 'Reply-To: '.$field_email."\r\n";

     $mail_status = mail($mail_to, $subject, $body_message, $headers);

     if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'contact.html';
        </script>
     <?php
     }
     else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to info@littlerivermiami.com');
            window.location = 'contact.html';
        </script>
     <?php
     }
 ?>

我确实看到提示消息,表示代码有效,但我没有收到来自表单填写信息的电子邮件。

任何想法为什么会发生?

2 个答案:

答案 0 :(得分:0)

我之前在没有MIME-Version和content-type的情况下向gmail和outlook发送电子邮件时遇到了问题。尝试添加:

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/plain; charset=utf-8\r\n"; 

答案 1 :(得分:0)

尝试从$headers删除mail(。你有一个活跃的垃圾邮件过滤器吗?也许您需要将From设置为您的服务器邮件地址,因为您的垃圾邮件过滤器不允许From设置为外部服务器的电子邮件地址。

注意:如果与Reply-To相同,则无需设置From