How to avoid this warning and notices when i use wrong password and email in my login form in imap_open:
Warning: 'imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX' and
Notice: Unknown: IMAP protocol error: Client aborted AUTHENTICATE command. i145-v6mb122753825lfi (errflg=2) in Unknown on line 0
Notice: Unknown: IMAP Authentication cancelled (errflg=2) in Unknown on line 0
I need some check when i use wrong password and email, close imap connection and echo smth like this 'wrong password or email!'. I tried to evoid this, but it is not working. Please tell me, what i am doing wrong.
function imap_open:
function open_mailbox($auth_user, $auth_password) {
$mailbox = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
if($imap = imap_open($mailbox, $auth_user, $auth_password)){
imap_open($mailbox, $auth_user, $auth_password);
echo "success";
} else {
imap_close($imap);
echo 'fail';
}
}
function of login form
function display_login_form($action) { ?>
<form method="post" action="index.php?action=log-in">
<div>
<div>
<div>
<p>LOGIN</p>
</div>
</br>
<div>
<div>
<div>
<p>MAIL</p>
</div>
<div>
<input type="text" name="username" required>
</div>
</div>
</br>
<div>
<div>
<p>PASSWORD</p>
</div>
<div>
<input type="password" name="password" required>
</div>
</div>
</br>
<div>
<input type='submit' value='LOGIN'>
</div>
</div>
</div>
</div>
</form>
index.php
$username = "";
$password = "";
if(isset($_POST['username']) && !empty($_POST['username'])){
$username = $_POST['username'];
}
if(isset($_POST['password']) && !empty($_POST['password'])){
$password = ($_POST['password']);
}
if(null!==($username && $password)) {
if(open_mailbox($username, $password)) {
$action = 'view-mailbox';
$_SESSION['auth_user'] = $username;
$_SESSION['auth_password'] = $password;
} else {
$status .= "<p>Wrong password or email</p>";
}
}
You can use imap_errors()
to get rid of warnings and notices:
When imap_errors() is called, the error stack is subsequently cleared.
Simply call it before calling imap_close()
.
Also note, that you call imap_open()
twice in this block, once in the assignment inside the if condition, and once right after it:
if($imap = imap_open($mailbox, $auth_user, $auth_password)){
imap_open($mailbox, $auth_user, $auth_password);
echo "success";
}
This is not necessary. If the $imap = imap_open($mailbox, $auth_user, $auth_password)
assignment does not return FALSE
, you do not need to open the imap once more.