1: <?php
2:
3: /**
4: * Avisota newsletter and mailing system
5: *
6: * PHP Version 5.3
7: *
8: * @copyright bit3 UG 2013
9: * @author Tristan Lins <tristan.lins@bit3.de>
10: * @package avisota-core
11: * @license LGPL-3.0+
12: * @link http://avisota.org
13: */
14:
15: namespace Avisota\Test\Imap;
16:
17: class ImapMailboxChecker
18: {
19: /**
20: * @param resource $imapConnection
21: * @param array $messages
22: * @param bool $clean
23: *
24: * @return bool Return <em>true</em> if <strong>all</strong> messages exist in the inbox.
25: */
26: public function checkMessages($imapConnection, array $messages, $clean = true)
27: {
28: $bodies = array_map(
29: function ($message) {
30: return $message->getText() . "\r\n";
31: },
32: $messages
33: );
34: $host = $_ENV['AVISOTA_TEST_IMAP_HOST'] ? : getenv('AVISOTA_TEST_IMAP_HOST');
35: $hits = 0;
36:
37: for ($i=0; $i<30 && $hits < count($bodies); $i++) {
38: // wait for the mail server
39: sleep(2);
40:
41: imap_gc($imapConnection, IMAP_GC_ENV);
42: $status = imap_status($imapConnection, '{' . $host . '}', SA_MESSAGES);
43:
44: for ($j = $status->messages; $j > 0; $j--) {
45: $body = imap_body($imapConnection, $j);
46:
47: if (in_array($body, $bodies)) {
48: $hits++;
49:
50: if ($clean) {
51: imap_delete($imapConnection, $j);
52: }
53: }
54: }
55:
56: imap_expunge($imapConnection);
57: }
58:
59: return $hits;
60: }
61: }
62: