3

I am trying to make a connection using imap_open and I am trying to return the error message to the end-user. However, the execution stopped every time whenever it encountered an error.

try {
    $mbox=imap_open( "{" . $data['imap_host'] . ":" . $data['imap_port'] . "/novalidate-cert}INBOX", $data['email'], $data['imap_password'] );
    imap_close($mbox);
    return [
        'success' => true
    ];

}catch (Exception $e) {
    return [
        'success' => false,
        'message' => $e->getMessage() //imap_last_error()
    ];
}

With above code, it stops with error

"message": "imap_open(): Couldn't open stream {mail.example.com:143/novalidate-cert}INBOX",
    "exception": "ErrorException",

I have tried by adding @imap_open, this suppressed the error that's why it didn't return any error message on $e->getMessage(). I tried replacing $e->getMessage() with imap_last_error() but nothing happens.

What am I missing? How could I catch the error, if any encountered during the process?

Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45
  • I don't see any code after this, and the message you're receiving is not what would normally be being thrown. So it's being caught and processed somewhere. – aynber Oct 31 '19 at 12:57
  • @aynber I am pretty sure the error is being generated from the exact line as there it shows the same line number. This error will be thrown, whenever the stream could not be opened. For ex: When I enter wrong password, it throws this error and stops there. – Saroj Shrestha Oct 31 '19 at 13:16
  • @SarojShrestha Can you verify if the error thrown is a `Fatal error` ? In that case you cannot use try-catch. Fatal errors cannot be caught. – Techno Oct 31 '19 at 13:39
  • @RobBiermann This error would be generated when I enter the wrong password if the entered password is the correct one, then there won't be any error. As per the error message, it's showing `ErrorException` – Saroj Shrestha Oct 31 '19 at 14:51
  • Can you show us the full error stack in your question? – Techno Oct 31 '19 at 14:52

0 Answers0