Else doesn’t work

Asked

Viewed 293 times

1

Hi. I have a website that I’ve uploaded the google recaptcha to. The captcha works correctly, however, I cannot make an error display if the message is sent. The "message sent successfully" notification is displayed normally, but the "message NOT sent" notification does not appear at all. However, the rest works normally, the message is only sent if the captcha is completed. The only problem is the notification of not sent.

Follows 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, only disappears when the email is sent, where it gives place to successful message in sending. It’s the only problem now. This message only needs to be hidden by default and display only when 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();
    ?>
  • When sending fails is showing success message? Or does it show anything? Pay attention to the displayed text as well, since you are using the "Alert-Success" class for the fault message.

  • When sending fails, shows nothing.

  • I only kept the Alert-Success class to test if that way it would work.

2 answers

1

The case that the Else is not triggered would be $mail->Send() does not return false in case of failure, suggest q see the function result using the var_dump($mail->Send()), as shown in the documentation, http://php.net/var_dump function will help to know the true return of function

1


You say that if CAPTCHA is not completed the message should not be sent. There is an external if that prevents an upload attempt if the field is not filled in. 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 CAPTCHA was provided and there was a problem with the method Send()

You can remove the internal if and add && $mail->Send() at the end of the external IF, thus:

    <?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 method Send() will not be called.

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

  • In this way the error message is displayed, however, the success message stopped being displayed and the page enters an infinite loading after sending attempt.

  • It doesn’t make sense, because the code I posted does exactly what the previous one did, only it uses the same IF. Make sure there was no problem with sending the email. Infinite loop without a for or while does not exist. It may be that your script is waiting for the sending server to reply.

  • And another detail that I just realized, the error message is displayed all the time.

  • Check if the upload is occurring. This code will only display success if all the above conditions are true, and the return of $mail->Send() is also true.

  • The email is not being sent.

  • Edit your question and put the code, here it is unscathed.

  • I was able to partially solve the problem. The sendmail file was with a change that I had made and had not removed and so the infinite load. The only problem now is that before sending the email, the error message already appears. of email not sent.

  • The logic I gave you is correct. Maybe you should look at the documentation of this lib you are using to send email. And check the return of the Send() function. My solution was given based on the code you put in the question. I don’t know this library and problems related to it I don’t know how to solve.

  • I’ll take a look. Because it is now working correctly, however, the email error message not sent appears all the time, as if the Else was already called before any part of the form was even filled in.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.