Else does not work

1

Hello. I have a website where I have inserted the google recaptcha. The captcha works correctly, however, I can not display an error if the message is sent. The "successfully sent message" notification is displayed normally, but the "NOT sent message" notification does not appear at all. However, the rest works normally, the message is only sent if the captcha is filled. The only problem is notification of not sent.

Follow the code

                    <div class="row">
                    <!--=== Contact Form ===-->
                    <form id="contact-form" class="col-sm-8 col-sm-offset-2" method="post" novalidate>
                        <div class="form-group">
                            <label class="control-label" for="contact-name">Nome</label>
                            <div class="controls">
                                <input id="contact-name" name="nome" placeholder="Seu nome" class="form-control requiredField" type="text" data-error-empty="Por favor entre com seu nome">
                            </div>
                        </div><!-- End name input -->

                        <div class="form-group">
                            <label class="control-label" for="contact-mail">Email</label>
                            <div class=" controls">
                                <input id="contact-mail" name="email" placeholder="Seu email" class="form-control requiredField" type="email" data-error-empty="Por favor entre com seu email" data-error-invalid="Invalid email address">
                            </div>
                        </div><!-- End email input -->

                        <div class="form-group">
                            <label class="control-label" for="contact-message">Mensagem</label>
                            <div class="controls">
                                <textarea id="contact-message" name="mensagem"  placeholder="Sua mensagem" class="form-control requiredField" rows="8" data-error-empty="Por favor entre com seu mensagem"></textarea>
                            </div>
                        </div><!-- End textarea -->
            <div class="g-recaptcha" data-sitekey="6LcvSg8UAAAAAAsOYcKHl2mxO_Uq-e7e9X58sc_I"></div>
                        <p class="text-center"><button name="submit" type="submit" class="btn btn-quattro" data-error-message="Error!" data-sending-message="Enviando..." data-ok-message="Mensagem Enviada"><i class="fa fa-paper-plane"></i><?= $modulo9->modulo9_button ?></button></p>
                        <input type="hidden" name="submitted" id="submitted" value="true" />

                    </form><!-- End contact-form -->
                    <?php
                    if (isset($_POST['email']) && !empty($_POST['g-recaptcha-response']) && !empty($_POST['email'])) {
                        require_once  'sendmail.php';
                        if ($mail->Send()) {
                            echo "<p class='alert alert-success' id='msg_alert'> <strong>Obrigado !</strong> Seu e-mail foi entregue.</p>";
                        } else {
                            echo "<p class='alert alert-success' id='msg_alert'> <strong>Obrigado !</strong> Seu e-mail NÃO foi entregue.</p>";
                        }
                    }
                    ?> 
                </div>
            </div>
        </section>
    <?php endif; ?>

Thank you in advance.

The error is now another, the error message is displayed all the time, it only disappears when the email is sent, where it gives place to the message of success in sending. It's the only problem now. This message only needs to be hidden by default and display only when the else is called, but is displaying all the time.

What I'm using is:

    <?php
    require_once  'sendmail.php';
    if (isset($_POST['email']) && !empty($_POST['g-recaptcha-response']) && !empty($_POST['email'])  && $mail->Send()) {
        echo "<p class='alert alert-success' id='msg_alert'> <strong>Obrigado !</strong> Seu e-mail foi entregue.</p>";
    } else {
        echo "<p class='alert alert-danger' id='msg_alert'> <strong>Email não enviado.</strong> Verifique o Captcha e os outros campos.</p>";
    }
?> 

The library is phpmailer

The sendmail.php:

<?php
        require_once './loader.php';
        require_once './plugin/email/email.php';
        global $mail;
        $smtp = new Smtpr();
        $smtp->getSmtp();
        $mail->Port = $smtp->smtp_port;
        $mail->Host = $smtp->smtp_host;
        $mail->Username = $smtp->smtp_username;
        $mail->From = $smtp->smtp_username;
        $mail->Password = $smtp->smtp_password;
        $mail->FromName = $smtp->smtp_fromname;
        $mail->Subject = utf8_decode("Contato Via Site " . $site->site_meta_titulo);
        $mail->AddBCC($smtp->smtp_bcc);
        $mail->AddAddress($smtp->smtp_username);

        $data = date('d/m/Y H:i');
        $nome = $_POST['nome'];
        $email = $_POST['email'];
        $mensagem = $_POST['mensagem'];

        $mail->AddReplyTo($email);
        $body = "<b>Data da Mensagem: </b> $data <br />";
        $body .= "<b>Nome:</b> $nome <br />";
        $body .= "<b>E-mail:</b> $email <br />";
        $body .= "<b>Mensagem: </b>$mensagem <br />";
        $mail->Body = nl2br($body);
        //$mail->Send();
    ?>
    
asked by anonymous 28.12.2016 / 18:50

2 answers

1

You say that if the CAPTCHA is not filled the message should not be sent. There is an external if that prevents a send attempt if the field is not filled. However, there is no else for this IF, and that is where the failure message should be displayed. You are showing this message only when the CAPTCHA was provided and there was a problem with the method Send()

You can remove the inner if and add && $mail->Send() to the end of the external IF, like this:

    <?php
        require_once  'sendmail.php';
        if (isset($_POST['email']) && !empty($_POST['g-recaptcha-response']) && !empty($_POST['email'])  && $mail->Send()) {
            echo "<p class='alert alert-success' id='msg_alert'> <strong>Obrigado !</strong> Seu e-mail foi entregue.</p>";
        } else {
            echo "<p class='alert alert-success' id='msg_alert'> <strong>Obrigado !</strong> Seu e-mail NÃO foi entregue.</p>";
        }
    ?> 

Remembering that if one of the above conditions is false, the Send() method will not be called.

You can also replicate% s of the internal if% to the external if, but it can be a problem if you want to change the message and do not remember that it is defined in two places.

    
28.12.2016 / 19:52
1

The case of the else not being triggered would be because the $ mail-> Send () does not return false on failure, I suggest you see the result of the function using the var_dump ( $ mail-> Send ()) , as shown in the documentation, link the function will help to know the true return of function

    
28.12.2016 / 19:36