联系表格php不发送电子邮件

时间:2016-04-25 11:09:59

标签: php contact-form

我有一个contact.HTML文件,其中包含以下代码:

<div>
    <form id="email-form" name="email-form" action="mail.php" data-name="Email Form">
        <input class="w-input text-field" id="name" type="text" placeholder="Enter your full name" name="name">
        <input class="w-input text-field" id="email" type="email" name="email" placeholder="Enter your email address">
        <input class="w-input text-field" id="phone" type="text" name="phone" placeholder="Enter your telephone number">
        <textarea class="w-input text-area" id="text-area" name="message" required placeholder="Your message here..."></textarea>
        <div class="div-spc">
            <button class="w-button button no-margin" type="submit">Submit Message</button>
        </div>
    </form>
  <div id="result"></div>
</div>

和一个带有以下代码的文件mail.php:

<?php    
define( "RECIPIENT_NAME", "Site name" );
define( "RECIPIENT_EMAIL", "xxx@domainame.com" );

$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$phone = isset( $_POST['phone'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['phone'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, $message, $headers );
  echo "<p class='success'>Mail Sent!</p>";
}    
?>

表单不起作用...我在浏览器页面上显示消息email sent。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

邮件发送需要SMTP设置。请正确检查。

此外,您的代码会检查表单变量是否已过帐。

不检查邮件是否已发送。

将您的代码更改为:

$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, $message, $headers );
if ($success) {
 echo "<p class='success'>Mail Sent!</p>";
}