Php mail() function does not work!

Asked

Viewed 45 times

-3

I have a system inside the xampp I’m developing, I tried to use the php mail function to send a password recovery email, but it doesn’t work. Someone can help me there for days that I have with this problem.

Code:

<?php
session_start();
include('../classes/conexao.php');

if (empty($_POST['email'])) {
    $_SESSION['campos'] = true;
    header('Location: ../view/recupera_senha.php');
    exit();
} else {

    $email = mysqli_real_escape_string($conexao, trim($_POST['email']));

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $_SESSION['invalido'] = true;
        header('Location: ../view/recupera_senha.php');
        exit();
    } else {

        $query = "SELECT COUNT(*) AS reg FROM usuarios WHERE email = '$email'";
        $result = mysqli_query($conexao, $query);
        $row = mysqli_fetch_assoc($result);

        if ($row['reg'] == 1) {

            $novasenha = substr(md5(time()), 0, 6);
            $senhacriptografada = md5(md5($novasenha));

            try {      
                if(mail($email, "Title", "Mensagem", "Cabeçalho")) {
                    $query = "UPDATE usuarios SET senha = '$senhacriptografada' WHERE email = '$email'";
                    $result = mysqli_query($conexao, $query);  

                    $_SESSION['enviado'] = true;
                    header('Location: ../view/recupera_senha.php');
                    exit();
                }             

            } catch (Exception $e) {
                echo 'Exceção capturada: ',  $e->getMessage(), "\n";
            }
        } else {
            $_SESSION['inexistente'] = true;
            header('Location: ../view/recupera_senha.php');
            exit();
        }
    }          
}

?>

1 answer

0

Guys I managed to solve, the problem was that my gmail that was in php sendmail was not allowed to send external email. So I had to configure my gmail to send. There was no problem in the code.

  • mail($email, "Title", "Mensagem", "Cabeçalho"), that $Cabeçalho didn’t make any sense in the code.

Browser other questions tagged

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