How to check the return of a function

0

Galera I put together a function that executes the class phpmailer and sends an e-mail. But I do not know how to check her return and find out if the email was sent. Good follows function:

    function envia_email($destinatario) {

        // Aqui fica todos os dados da função


        // Envia o e-mail
        $email->Send();
    }

What sends the email and this command $email->Send(); .

And I call the function like this:

envia_email($destinatario);

Well, how do I know if it was true or false?

    
asked by anonymous 22.07.2016 / 13:38

3 answers

1

You need to review the library you are using, take a look at its documentation, and see the% return of%.

However, if you are using send() that is usually used you can see here:

  

Creates message and assigns Mailer. If the message is not sent correctly then it returns false. Use the ErrorInfo variable to view description of the error. Returns true on success, false on failure.

This method returns PHPMailer or true for sending. So you can use this return.

function envia_email($destinatario) {

    // Aqui fica todos os dados da função


    // Envia o e-mail
    return $email->Send(); // true or false
}

$retorno = envia_email($destinatario);

if($retorno === false){
   throw new Exception('Email não foi enviado.');
}

Simple.

    
22.07.2016 / 14:10
0

It returns True or False:

if(!$Email->Send()) {
   echo A mensagem não foi enviada.";
   echo Mensagem de erro: " . $Email->ErrorInfo;
} else {
    echo "Mensagem enviada!";
}

More details of the class operation here: link

    
22.07.2016 / 13:41
0

I use this code, it works well and reports the errors.

if(!$email->Send()) :
$mensagemRetorno = 'Erro ao enviar formulário: '. print($email->ErrorInfo);
else : 
echo"<script>alert ('Email enviado!')</script>";
endif;
    
22.07.2016 / 13:55