Wordpress自定义表单,提交后加载错误的模板,不能发送邮件

1 人关注

我试图为一个Wordpress网站建立一个简单的联系表格。我想让它发送一封电子邮件,并重新加载它所发送的联系页面,并给出错误/成功的信息。

我已经安装了WP邮件的SMTP,它可以发送测试邮件。

现在说说问题。我有一个我想使用的html格式的表单。这个表单被放在一个自定义模板中。

php是借用了我在网上找到的指南,我用过其中的几个。他们都不适合我。当我点击提交按钮时,页面加载的是 基本模板 (而不是我为表单页面使用的自定义模板),没有邮件被发送。我想让它加载同样的页面,但有一个错误/成功的 div 告诉我发生了什么。如果我在按下提交键之前运行php,它会给我一个错误信息。

这里是我的php。

if(isset($_POST['submitButton'])){
    //response generation function
$response = "";
    //function to generate response
function my_contact_form_generate_response($type, $reason){
      global $response;
      if($type == "success") $response = "<div class='success'>{$reason}</div>";
      else $response = "<div class='error'>{$reason}</div>";
    //response messages
$not_human       = "Human verification incorrect.";
    $missing_content = "Please supply all information.";
    $email_invalid   = "Email Address Invalid.";
    $message_unsent  = "Message was not sent. Try Again.";
    $message_sent    = "Thanks! Your message has been sent.";
    //user posted variables
$name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $phone = $_POST['phone'];
    $human = 2;
    //php mailer variables
$to = get_option('admin_email');
    $subject = "Someone sent a message from ".get_bloginfo('name');
    $headers = 'From: '. $email . "\r\n" .
      'Reply-To: ' . $email . "\r\n";
        if(!$human == 0){
        if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
            //validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            my_contact_form_generate_response("error", $email_invalid);
            else //email is valid
            //validate presence of name and message
if(empty($name) || empty($message)){
                my_contact_form_generate_response("error", $missing_content);
            else //ready to go!
                $sent = mail($to, $subject, strip_tags($message), 
$headers);
            if($sent) my_contact_form_generate_response("success", 
$message_sent); //message sent!
else my_contact_form_generate_response("error", 
$message_unsent); //message wasn't sent
    else if ($_POST['submitted']) 
my_contact_form_generate_response("error", $missing_content);

这是我为 "打印错误/成功 "和表格设计的HTML。

<?php echo $response; ?>
<form class="interest-form" action="<?php the_permalink(); ?>" method="POST">
    <fieldset>
        <legend>Anmäl Intresse:</legend>
        <div class="row">
            <div class="col-6">
                <input type="text" class="form-control" placeholder="Namn" name="name" value="<?php echo esc_attr($_POST['name']); ?>">
            </div>
            <div class="col-6">
                <input type="tel" class="form-control" placeholder="Telefonnummer" name="phone" value="<?php echo esc_attr($_POST['phone']); ?>">
            </div>
        </div>
        <div class="row">
            <div class="col">
                <input type="email" class="form-control" placeholder="E-post" name="email" value="<?php echo esc_attr($_POST['email']); ?>">
            </div>
            <div class="col">
                <input class="form-control" type="text" placeholder="<?php the_field('fb_8_1_plats'); ?> <?php the_field('fb_8_1_startdatum'); ?>" readonly name="plats_datum">
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="form-group">
                    <label for="meddelande1">Meddelande:</label>
                    <textarea class="form-control" rows="3" name="message" value="<?php echo esc_attr($_POST['message']); ?>" id="meddelande1"></textarea>
                </div>
            </div>
        </div>
        <input type="hidden" name="submitted" value="1">
        <button type="submit" name="submitButton" class="btn btn-primary float-right">Skicka intresseanmälan</button>