简体   繁体   English

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

[英]Contact form php not send email

I have a contact.HTML file with follow code: 我有一个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>

and a file mail.php with follow code: 和一个带有以下代码的文件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>";
}    
?>

The form doesn't work... and I have message email sent on my page on the browser. 表格无法使用...,并且我在浏览器的页面上email sent了邮件email sent Anyone can help me? 有人可以帮助我吗?

Mail sending needs SMTP settings. 邮件发送需要SMTP设置。 Please check them correctly. 请正确检查。

Also, your code checks if form variables are posted or not. 另外,您的代码还会检查是否发布了表单变量。

It does not check whether the mail is sent or not. 它不检查邮件是否已发送。

Change your code to: 将您的代码更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM