-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimapfilter.class.php
164 lines (130 loc) · 3.94 KB
/
imapfilter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class IMAPFilter {
private $imap_conn;
private $default_mailbox;
private $default_mailbox_server;
function __construct($username, $password, $server, $mailbox="INBOX", $ssl=true, $port=993) {
$this->imap_conn = false;
$this->default_mailbox = $mailbox;
if($ssl)
$this->default_mailbox_server = "{".$server.":".$port."/imap/ssl}";
else
$this->default_mailbox_server = "{".$server.":".$port."/imap}";
$this->imap_conn = imap_open ($this->default_mailbox_server.$this->default_mailbox, $username, $password);
if(!$this->imap_conn)
throw new Exception("IMAP connection failed");
}
function __destruct() {
$this->closeConnection();
}
/**
* closeConnection
* Closes the current connection, if opened.
*
* @return void
*/
public function closeConnection() {
if($this->imap_conn != false)
imap_close($this->imap_conn);
$this->imap_conn = false;
}
/**
* imapSearch
* Internal helper function for convience
* @param string $filter See: http://php.net/manual/de/function.imap-search.php criteria
*
* @return array or single element
*/
private function imapSearch($filter) {
$result = imap_search($this->imap_conn, $filter);
if(!$result)
return false;
if(count($result) > 1)
return $result;
if(count($result) == 1) {
$result = $result[0];
return $result;
}
return false;
}
/**
* searchInMailbox
* Searchs thorugh an mailbox all mails that matches the criteria and returns the data as string in RFC2060 format for use with PHP
* @param string $filter See: http://php.net/manual/de/function.imap-search.php criteria
*
* @return string RFC2060 message id list
*/
public function searchInMailbox($filter) {
$result = $this->imapSearch($filter);
$return = "";
if(!$result)
return false;
if(is_array($result)) {
foreach($result as $mail_id) {
$return .= $mail_id.",";
}
$return = substr($return, 0, strlen($return)-1);
return $return;
}
if($result != "")
return $result;
else
return false;
}
/**
* markMessagesAsSeen
*
* Will automatically mark every message found with $filter as seen.
* @param string $filter See: http://php.net/manual/de/function.imap-search.php criteria
*
* @return bool
*/
public function markMessagesAsSeen($filter) {
$result = $this->searchInMailbox($filter);
if(!$result)
return false;
return imap_setflag_full($this->imap_conn, $result, "\\Seen");
}
/**
* changeMailBox
* Changes the current mailbox on an open connection
* @param string $mailbox Mailbox you would like to change to, e.g. INBOX.Work
*
* @return bool
*/
public function changeMailbox($mailbox) {
if($this->imap_conn != false) {
return imap_reopen($this->imap_conn, $this->default_mailbox_server.$mailbox);
}
}
/**
* markMailboxAsSeen
* Marks all mail's inside a mailbox as seen
* @param <type> $mailbox Mailbox where you'd like to set every message as seen e.g. INBOX.Work
*
* @return <type>
*/
public function markMailboxAsSeen($mailbox) {
if($this->changeMailbox($mailbox)) {
$this->markMessagesAsSeen("ALL");
return $this->changeMailbox($this->default_mailbox);
}
}
/**
* moveToMailBox
* Moves every message from the current mailbox, that matches the filter, to the destination mailbox.
* @param string $filter See: http://php.net/manual/de/function.imap-search.php criteria
* @param <type> $dest_mailbox Destination Mailbox, where you'd like to save the messages to e.g. INBOX.Work
*
* @return <type>
*/
public function moveToMailbox($filter, $dest_mailbox) {
$result = $this->searchInMailbox($filter);
if(!$result)
return false;
if(!imap_mail_move($this->imap_conn, $result, $dest_mailbox))
return false;
return imap_expunge($this->imap_conn);
}
}
?>