在php中检查未读邮件

I'm trying to implement a check new mail function to my page. By this I mean a script that checks the mail, if there exists unread mails it will notify the user "You got one unread mail".

Is this possible?

Thanks in advance

I managed to get it to work as long as the users emails and passwords are stored as plain text in the database.

I'm using a query to retrieve email and password of a user from my database ($email) and ($password)

The code:

$mbox = imap_open("{imap.domain.com:143/novalidate-cert}INBOX", "$email", "$password");

The only problem is that the email passwords for my users are stored as md5 hash.

How can I handle this with imap_open?

Thanks

It is possible, if you implement an IMAP (or POP3) client in your PHP script. When you open your page, PHP would connect to the mail server and check for new messages. To achieve this, PHP would need your username/password and server address/port. Hence, this information will have to be stored on the server.

The example given at http://lv.php.net/imap_mailboxmsginfo will give you some more hints.

You'll need to code in IMAP support, which can be done using the PHP imap functions. A complete solution isn't trivial though.

You can comfortably do that using the Zeta Mail component, even without any special extension being available.

$hostname='{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'mygmail@gmail.com';
$password = 'mypass';

$mbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
$status=imap_status($mbox,$hostname,SA_ALL);
if ($status) {
  echo "Messages:   " . $status->messages    . "<br />
";
  echo "Recent:     " . $status->recent      . "<br />
";
  echo "Unseen:     " . $status->unseen      . "<br />
";
  echo "UIDnext:    " . $status->uidnext     . "<br />
";
  echo "UIDvalidity:" . $status->uidvalidity . "<br />
";
} 
else {
  echo "imap_status failed: " . imap_last_error() . "
";
}

$hostname='{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'mygmail@gmail.com';
$password = 'mypass';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
$MB = imap_search($inbox,'UNSEEN');
$xcount($MB);
echo $x;
</div>

If you can't use imap_open (the extension is not installed, for example), you can use curl (example tested with gmail):

// https://support.google.com/mail/answer/7126229 [2017-10-22]
define('URL', 'imaps://imap.gmail.com');
define('PORT', 993);
define('USER', 'your.user@gmail.com');
define('PASS', 'your_Secret_Password');

if ($ch = curl_init()) {
    curl_setopt($ch, CURLOPT_URL, URL);
    curl_setopt($ch, CURLOPT_PORT, PORT);

    curl_setopt($ch, CURLOPT_USE_SSL, CURLUSESSL_ALL);

    curl_setopt($ch, CURLOPT_USERNAME, USER);
    curl_setopt($ch, CURLOPT_PASSWORD, PASS);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // set IMAP command
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'STATUS INBOX (MESSAGES UNSEEN)');

    $res = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'CURL ERROR: ' . curl_error($ch);
    } else {
        echo trim($res);
    }
    echo PHP_EOL;

    curl_close($ch);
} else {
    die('Curl initialization failed.');
}

The script will return something like:

* STATUS "INBOX" (MESSAGES 2 UNSEEN 1)

More about IMAP commands (https://www.google.com/search?q=imap+protocol+commands) [2017-10-22]: