How to catch error message from imap_open?

1.9k Views Asked by At

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?

0

There are 0 best solutions below