php - 我在 wordpress 中的 contact.php 表单不发送消息

标签 php html wordpress contact

我有一个文件 contact.php,其中包含 PHP 代码和一个 html 表单,但是当我想发送消息时却不能。我试图在代码中找到错误代码或问题,但我都找不到。

这是我的整个 contact.php 文件,请帮我找出问题。

<?php 
// http://www.infotuts.com/bootstrap-3-contact-form/
// Function for email address validation
function isEmail($verify_email) {
        return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $verify_email)
    );
}

$error_name = false;
$error_email = false;
$error_message = false;

if (isset($_POST['contact-submit'])) {
    // Initialize variables for the form fields
    $name = '';
    $email = '';
    $website = '';
    $message = '';
    $receiver_email = '';

    // Get the name
    if (trim($_POST['contact-author']) == '') {
        $error_name = true;
    } else {
        $name = trim($_POST['contact-author']);
    }

    // Get the email
    if (trim($_POST['contact-email']) == '' || !isEmail(trim($_POST['contact-email']))) {
        $error_email = true;
    } else {
        $email = trim($_POST['contact-email']);
    }   

    // Get the website
        $website = trim($_POST['contact-url']); 

    // Get the message
    if (trim($_POST['contact-message']) == '') {
        $error_message = true;
    } else {
        $message = stripcslashes(trim($_POST['contact-message']));
    }   

    // Check if we have errors
    if (!$error_name && !$error_email && !$error_message) {
        // Get the received email
        $receiver_email = 'rerraw@gmail.com';

        $subject = 'You have been contacted by' . $name;
        $body = "You have been contacted by $name. Their Message is:". PHP_EOL . PHP_EOL;
        $body .= $message . PHP_EOL . PHP_EOL;
        $body .= "You can contact $name via email at $email.";

        if ($website != '') {
            $body .= "and visit their website at $website";
        }
        $body .= PHP_EOL . PHP_EOL;

        $headers = "From $email" . PHP_EOL;
        $headers .= "Reply-To: $email" . PHP_EOL;
        $headers .= "MIME-Version: 1.0" . PHP_EOL;
        $headers .= "Contetnt-type text/plain; charset=utf-8" . PHP_EOL;
        $headers .= "Content-Transfer-Encoding: qouted-printable" . PHP_EOL;

        if (wp_mail($receiver_email, $subject, $body, $headers)) {
            $email_sent = true;
        } else {
            $email_sent_error = true;
        }

    }       
}   
?>

<?php if (isset($email_sent) && $email_sent = true) : ?>
<h3><?php _e('Success', 'Yallanpe-Theme'); ?></h3>
<p><?php _e('You have successfully sent the message.', 'Yallanpe-Theme'); ?></p>
<?php elseif (isset($email_sent_error) && $email_sent_error = true) : ?>
<h3><?php _e('Error!', 'Yallanpe-Theme'); ?></h3>
<p><?php _e('We coudnt send the message at this time please try again later.',     'Yallanpe-Theme'); ?></p>
<?php else : ?>
<div id="contact-us" class="col-lg-4 col-md-6 col-sm-6 col-xs-12">
<h4>Contact Us</h4>
<form action="" method="POST" id="contact-form" role="form">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input value="<?php if (isset($_POST['contact-author'])) echo $_POST['contact-author'];  ?>" type="text" class="form-control" name="contact_author" id="contact_author"  placeholder="First Name" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>   </span>
<input value="<?php if (isset($_POST['contact-email'])) echo $_POST['contact-eamil']; ?>" type="text" class="form-control" name="contact_email" id="contact_email" placeholder="Email Address" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input value="<?php if (isset($_POST['contact-url'])) echo $_POST['contact-url']; ?>" type="text" class="form-control" name="contact_url" id="contact_url" placeholder="Wbsite URL" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-comment"></span></span>      <textarea class="form-control" name="contact_message" id="contact_message" cols="60" rows="5" placeholder="Write Your Message Here"><?php if (isset($_POST['contact-message'])) echo $_POST['contact-message']; ?></textarea>
</div>
</div>
<input type="hidden" name="contact-submit" id="contact-submit" value="true" />
<input type="submit" class="btn btn-primary" value="Send Message" />
</form>
</div>
<?php endif; ?>

最佳答案

  1. 使用正确 header “来自$email”无效!标题的有效名称-值对是名称: 值\r\n

  2. 如果您使用 WordPress,请使用 wp_mail 而不是 ma​​il,否则请使用 PHPMailer 类。如果您使用 wp_mail,请将 header 设置为 Array

  3. 正确验证您的输入。不要使用你的函数 isEmail,使用 PHP 自己的 filter_var

  4. 收不到邮件?试试看:转到 wp-login.php 并重置您的密码!邮件仍未发送,您的服务器未配置为发送邮件联系您的服务器管理员!

关于php - 我在 wordpress 中的 contact.php 表单不发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596086/

相关文章:

PHP - 获取远程文件的 md5?

php - ajax url 使用特定单词 "Altar "或 "altar"给出 403 其他 url 工作正常

javascript - 如果其他打开,jQuery 关闭 ul

php - single-post.php 无法在实时 WordPress 网站上运行?

php - 使用范围和关系计算两行 (Laravel)

php - 什么是裁剪图像以避免空白或空白区域的好算法或库?

html - HTML5 音频标签是否支持从另一个域提供 mp3?

javascript - canvas.drawImage 使裁剪后的图像不成比例

html - Internet Explorer 上的 CSS 错误阴影效果(纸张 curl )

javascript - 为 wordpress 菜单项添加不同的属性