'HTML + PHP + PHPMAILER

obs: using translator

I have a problem on my website, every time this is sending the normal email, but it looks like an error message, in the template I got, it has a send div, as I do to skip the error-message and go to send -error

HTML CODE:

<div class="my-3">
                <div class="loading">Enviando</div>
                <div class="error-message"></div>
                <div class="sent-message">Sua mensagem foi enviada com sucesso. Muito Obrigado!</div>
              </div>

PHP CODE (PHPMAILER):

function smtp_mailer($para, $de, $de_nome, $assunto, $corpo) { 
    global $error;
    $mail = new PHPMailer();
    $mail->IsSMTP();        // Ativar SMTP
    $mail->SMTPDebug = 3;       // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
    $mail->SMTPAuth = true;     // Autenticação ativada
    $mail->SMTPSecure = 'ssl';  // SSL REQUERIDO pelo GMail
    $mail->Host = 'smtp.gmail.com'; // SMTP utilizado
    $mail->Port = 465;          // A porta 587 deverá estar aberta em seu servidor
    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Username = '*';
    $mail->Password = '*';
    $mail->SetFrom("*");
    $mail->Subject = $assunto;
    $mail->Body = $corpo;
    $mail->AddAddress($para);
    if(!$mail->Send()){
        echo $mail->ErrorInfo;
    }else {
        $error = 'Mensagem enviada!';
        return false;
    }
}

 if (smtp_mailer('*', '*', $Nome, $Assunto2, $Vai)) {

    return $send;

}
if (!empty($error)){
    echo $error;
}
?>

IMAGE SITE: enter image description here

mensagem enviada is message send in pt-br.

I need only jump for div sent-message :)



Solution 1:[1]

You do the error catching wrong, do this and it will work when you are prosting to the php script with a form post. This will do nothing when you are using a javascript ajax call.

 if(!$mail->Send()){
        echo $mail->ErrorInfo;
        $error = 'Mensagem enviada!';
        return false;
    } else {
        return true;
    }

and

 if (smtp_mailer('*', '*', $Nome, $Assunto2, $Vai)) {

    echo "Done"; //dont return...

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1