How to check the return of a function

Asked

Viewed 708 times

0

Galera set up a function that executes the class phpmailer and send an email. However I do not know how to check her return and know if the email was sent. Good follows the 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 it that:

envia_email($destinatario);

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

3 answers

1


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

However, if you are using PHPMailer which it is customary to use you can observe here:

Creates message and assigns Mailer. If the message is not sent successfully then it Returns false. Use the Errorinfo variable to view Description of the error. Returns true on Success, false on Failure.

This method it returns true or false for sending. Therefore, 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.

  • OK, it worked 100% thank you very much

0

  • I know, but what I want is to check with the function, namely that the $Email->Send() retome false or true in the understood function.

0

I use this code, works well and reports errors.

if(!$email->Send()) :
$mensagemRetorno = 'Erro ao enviar formulário: '. print($email->ErrorInfo);
else : 
echo"<script>alert ('Email enviado!')</script>";
endif;
  • I know, but what I want is to check with the function, namely that the $Email->Send() retome false or true in the understood function.

Browser other questions tagged

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