SHOW:
|
|
- or go back to the newest paste.
1 | - | <?php |
1 | + | <?php |
2 | - | /** |
2 | + | /** |
3 | - | * Example usage: |
3 | + | * Example usage: |
4 | - | * |
4 | + | * |
5 | - | * // Initalize the client |
5 | + | * // Initalize the client |
6 | - | * $imap = DIOIMAP::init(); |
6 | + | * $imap = DIOIMAP::init(); |
7 | - | * |
7 | + | * |
8 | - | * // Initalize the email client without a postfix and search for the latest unread email |
8 | + | * // Initalize the email client without a postfix and search for the latest unread email |
9 | - | * $emailMessage = DIOIMAP::init()->findEmail(); |
9 | + | * $emailMessage = DIOIMAP::init()->findEmail(); |
10 | - | * |
10 | + | * |
11 | - | * // Reinitalize the stream, and find the latest email with the subject "Test 123" |
11 | + | * // Reinitalize the stream, and find the latest email with the subject "Test 123" |
12 | - | * $secondEmail = $imap->reinit()->findEmail("Test 123"); |
12 | + | * $secondEmail = $imap->reinit()->findEmail("Test 123"); |
13 | - | * |
13 | + | * |
14 | - | * // Occasionally, the stream needs to be reset if the instance has done nothing for a while |
14 | + | * // Occasionally, the stream needs to be reset if the instance has done nothing for a while |
15 | - | * $imap = $imap->reinit(); |
15 | + | * $imap = $imap->reinit(); |
16 | - | * $imap->reinit()->findEmail(); |
16 | + | * $imap->reinit()->findEmail(); |
17 | - | * |
17 | + | * |
18 | - | */ |
18 | + | */ |
19 | - | class DIOIMAP { |
19 | + | class DIOIMAP { |
20 | - | const _IMAP_HOST = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX'; |
20 | + | const _IMAP_HOST = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX'; |
21 | - | private $imapStream; |
21 | + | private $imapStream; |
22 | - | private $user; |
22 | + | private $user; |
23 | - | private $password; |
23 | + | private $password; |
24 | - | private $expectedRecipient; |
24 | + | private $expectedRecipient; |
25 | - | private $subject; |
25 | + | private $subject; |
26 | - | private $latestId; |
26 | + | private $latestId; |
27 | - | |
27 | + | |
28 | - | private static $lastPostfix = ''; |
28 | + | private static $lastPostfix = ''; |
29 | - | /** |
29 | + | /** |
30 | - | * Bootstrap a DIOIMAP object |
30 | + | * Bootstrap a DIOIMAP object |
31 | - | * @param $postfix (string) RFC 2822 email tag ex: $postfix = "testTag" -> $expectedRecipient = "myGmailAcct+testTag@gmail.com" |
31 | + | * @param $postfix (string) RFC 2822 email tag ex: $postfix = "testTag" -> $expectedRecipient = "myGmailAcct+testTag@gmail.com" |
32 | - | */ |
32 | + | */ |
33 | - | public static function init($postfix = '') |
33 | + | public static function init($postfix = '') |
34 | - | { |
34 | + | { |
35 | - | $key = 'myGmailAcct%s@gmail.com'; |
35 | + | $key = 'myGmailAcct%s@gmail.com'; |
36 | - | $user = sprintf($key, ""); |
36 | + | $user = sprintf($key, ""); |
37 | - | $expectedRecipient = empty($postfix) ? $user : sprintf($key, "+$postfix"); |
37 | + | $expectedRecipient = empty($postfix) ? $user : sprintf($key, "+$postfix"); |
38 | - | self::$lastPostfix = $postfix; |
38 | + | self::$lastPostfix = $postfix; |
39 | - | |
39 | + | |
40 | - | return new self($user, 'password!@', $expectedRecipient); |
40 | + | return new self($user, 'password!@', $expectedRecipient); |
41 | - | } |
41 | + | } |
42 | - | |
42 | + | |
43 | - | /** |
43 | + | /** |
44 | - | * @param $user (string) IMAP Server username |
44 | + | * @param $user (string) IMAP Server username |
45 | - | * @param $pass (string) IMAP Server password |
45 | + | * @param $pass (string) IMAP Server password |
46 | - | * @param $expectedRecipient (string) Expected recipient (in the "TO" field) of a set of email in the inbox |
46 | + | * @param $expectedRecipient (string) Expected recipient (in the "TO" field) of a set of email in the inbox |
47 | - | */ |
47 | + | */ |
48 | - | public function __construct($user, $pass, $expectedRecipient) |
48 | + | public function __construct($user, $pass, $expectedRecipient) |
49 | - | { |
49 | + | { |
50 | - | date_default_timezone_set("UTC"); |
50 | + | date_default_timezone_set("UTC"); |
51 | - | $this->user = $user; |
51 | + | $this->user = $user; |
52 | - | $this->password = $pass; |
52 | + | $this->password = $pass; |
53 | - | $this->expectedRecipient = $expectedRecipient; |
53 | + | $this->expectedRecipient = $expectedRecipient; |
54 | - | $this->openConnection(); |
54 | + | $this->openConnection(); |
55 | - | } |
55 | + | } |
56 | - | |
56 | + | |
57 | - | private static $TEMP_lastDate; |
57 | + | private static $TEMP_lastDate; |
58 | - | /** |
58 | + | /** |
59 | - | * Return the latest email id meeting the specified recipient and subject criteria |
59 | + | * Return the latest email id meeting the specified recipient and subject criteria |
60 | - | * @param $iteration (int) OPTIONAL The number used to identify the recursive position |
60 | + | * @param $iteration (int) OPTIONAL The number used to identify the recursive position |
61 | - | * @param $maxDepth (int) OPTIONAL The number used to limit the recursive depth. |
61 | + | * @param $maxDepth (int) OPTIONAL The number used to limit the recursive depth. |
62 | - | * @throws Exception No new emails found matching search criteria |
62 | + | * @throws Exception No new emails found matching search criteria |
63 | - | * @return (int) IMAP server id of the found email |
63 | + | * @return (int) IMAP server id of the found email |
64 | - | */ |
64 | + | */ |
65 | - | private function refresh($iteration = 0, $maxDepth = 5) |
65 | + | private function refresh($iteration = 0, $maxDepth = 5) |
66 | - | { |
66 | + | { |
67 | - | $theNow = new DateTime(); |
67 | + | $theNow = new DateTime(); |
68 | - | $emailStack = imap_search( |
68 | + | $emailStack = imap_search( |
69 | - | $this->imapStream, |
69 | + | $this->imapStream, |
70 | - | sprintf("TO %s %s UNSEEN NEW", |
70 | + | sprintf("TO %s %s UNSEEN NEW", |
71 | - | $this->expectedRecipient, |
71 | + | $this->expectedRecipient, |
72 | - | empty($this->subject) ? "" : sprintf('SUBJECT "%s"', $this->subject) |
72 | + | empty($this->subject) ? "" : sprintf('SUBJECT "%s"', $this->subject) |
73 | - | ), |
73 | + | ), |
74 | - | SE_UID |
74 | + | SE_UID |
75 | - | ); |
75 | + | ); |
76 | - | $this->close(); |
76 | + | $this->close(); |
77 | - | $this->openConnection(); |
77 | + | $this->openConnection(); |
78 | - | |
78 | + | |
79 | - | var_dump("Returned Email Stack"); |
79 | + | var_dump("Returned Email Stack"); |
80 | - | var_dump($emailStack); |
80 | + | var_dump($emailStack); |
81 | - | if (!$emailStack) |
81 | + | if (!$emailStack) |
82 | - | { |
82 | + | { |
83 | - | if ($iteration < $maxDepth) |
83 | + | if ($iteration < $maxDepth) |
84 | - | { |
84 | + | { |
85 | - | sleep(10); |
85 | + | sleep(10); |
86 | - | var_dump($this->imapStream); |
86 | + | var_dump($this->imapStream); |
87 | - | var_dump("Iteration: $iteration"); |
87 | + | var_dump("Iteration: $iteration"); |
88 | - | var_dump("Time diff:\n"); |
88 | + | var_dump("Time diff:\n"); |
89 | - | var_dump(self::$TEMP_lastDate->diff($theNow)->format("%h:%i:%s")); |
89 | + | var_dump(self::$TEMP_lastDate->diff($theNow)->format("%h:%i:%s")); |
90 | - | var_dump("\n\n"); |
90 | + | var_dump("\n\n"); |
91 | - | self::$TEMP_lastDate = $theNow; |
91 | + | self::$TEMP_lastDate = $theNow; |
92 | - | $this->refresh($iteration+1, $maxDepth); |
92 | + | $this->refresh($iteration+1, $maxDepth); |
93 | - | |
93 | + | |
94 | - | } |
94 | + | } |
95 | - | throw new Exception( |
95 | + | throw new Exception( |
96 | - | sprintf( |
96 | + | sprintf( |
97 | - | "No new emails found matching criteria: user=%s subject=%s attempt=%s depth=%s", |
97 | + | "No new emails found matching criteria: user=%s subject=%s attempt=%s depth=%s", |
98 | - | $this->expectedRecipient, |
98 | + | $this->expectedRecipient, |
99 | - | $this->subject, |
99 | + | $this->subject, |
100 | - | $iteration, |
100 | + | $iteration, |
101 | - | $maxDepth |
101 | + | $maxDepth |
102 | - | ) |
102 | + | ) |
103 | - | ); |
103 | + | ); |
104 | - | } |
104 | + | } |
105 | - | $this->latestId = array_pop($emailStack); // Get latest email only |
105 | + | $this->latestId = array_pop($emailStack); // Get latest email only |
106 | - | // foreach($emailStack as $mailId) |
106 | + | // foreach($emailStack as $mailId) |
107 | - | // { |
107 | + | // { |
108 | - | // imap_clearflag_full($this->imapStream, $mailId, "//Seen"); |
108 | + | // imap_clearflag_full($this->imapStream, $mailId, "//Seen"); |
109 | - | // } |
109 | + | // } |
110 | - | return $this->latestId; |
110 | + | return $this->latestId; |
111 | - | } |
111 | + | } |
112 | - | |
112 | + | |
113 | - | /** |
113 | + | /** |
114 | - | * Find and return the latest email meeting the specified recipient and subject criteria |
114 | + | * Find and return the latest email meeting the specified recipient and subject criteria |
115 | - | * @return (string) The email body |
115 | + | * @return (string) The email body |
116 | - | */ |
116 | + | */ |
117 | - | public function findEmail($subject = '') |
117 | + | public function findEmail($subject = '') |
118 | - | { |
118 | + | { |
119 | - | $this->subject = $subject; |
119 | + | $this->subject = $subject; |
120 | - | self::$TEMP_lastDate = new DateTime(); |
120 | + | self::$TEMP_lastDate = new DateTime(); |
121 | - | $this->refresh(0, 30); |
121 | + | $this->refresh(0, 30); |
122 | - | $emailBody = $this->fetch($this->latestId); |
122 | + | $emailBody = $this->fetch($this->latestId); |
123 | - | $emailBody = $this->decode($emailBody); |
123 | + | $emailBody = $this->decode($emailBody); |
124 | - | return $emailBody; |
124 | + | return $emailBody; |
125 | - | } |
125 | + | } |
126 | - | |
126 | + | |
127 | - | public function reinit() |
127 | + | public function reinit() |
128 | - | { |
128 | + | { |
129 | - | $this->close(); |
129 | + | $this->close(); |
130 | - | return self::init(self::$lastPostfix); |
130 | + | return self::init(self::$lastPostfix); |
131 | - | } |
131 | + | } |
132 | - | |
132 | + | |
133 | - | /** |
133 | + | /** |
134 | - | * Close the connection to the IMAP server |
134 | + | * Close the connection to the IMAP server |
135 | - | */ |
135 | + | */ |
136 | - | public function close() |
136 | + | public function close() |
137 | - | { |
137 | + | { |
138 | - | imap_close($this->imapStream); |
138 | + | imap_close($this->imapStream); |
139 | - | } |
139 | + | } |
140 | - | |
140 | + | |
141 | - | /** |
141 | + | /** |
142 | - | * Return the expected recipient |
142 | + | * Return the expected recipient |
143 | - | * @return (string) The expected recipient |
143 | + | * @return (string) The expected recipient |
144 | - | */ |
144 | + | */ |
145 | - | public function getRecipient() |
145 | + | public function getRecipient() |
146 | - | { |
146 | + | { |
147 | - | return $this->expectedRecipient; |
147 | + | return $this->expectedRecipient; |
148 | - | } |
148 | + | } |
149 | - | |
149 | + | |
150 | - | private function decode($emailBody) |
150 | + | private function decode($emailBody) |
151 | - | { |
151 | + | { |
152 | - | $structure = imap_fetchstructure($this->imapStream, $this->latestId); |
152 | + | $structure = imap_fetchstructure($this->imapStream, $this->latestId); |
153 | - | switch ($structure->encoding) |
153 | + | switch ($structure->encoding) |
154 | - | { |
154 | + | { |
155 | - | case 3: |
155 | + | case 3: |
156 | - | $emailBody = imap_base64($emailBody); |
156 | + | $emailBody = imap_base64($emailBody); |
157 | - | break; |
157 | + | break; |
158 | - | case 4: |
158 | + | case 4: |
159 | - | case 0: |
159 | + | case 0: |
160 | - | $emailBody = imap_qprint($emailBody); |
160 | + | $emailBody = imap_qprint($emailBody); |
161 | - | break; |
161 | + | break; |
162 | - | default: |
162 | + | default: |
163 | - | throw new Exception( |
163 | + | throw new Exception( |
164 | - | sprintf("Unknown encoding of email body encoding=%s", $structure->encoding) |
164 | + | sprintf("Unknown encoding of email body encoding=%s", $structure->encoding) |
165 | - | ); |
165 | + | ); |
166 | - | } |
166 | + | } |
167 | - | return $emailBody; |
167 | + | return $emailBody; |
168 | - | } |
168 | + | } |
169 | - | |
169 | + | |
170 | - | private function openConnection() |
170 | + | private function openConnection() |
171 | - | { |
171 | + | { |
172 | - | $this->imapStream = imap_open(self::_IMAP_HOST, $this->user, $this->password); |
172 | + | $this->imapStream = imap_open(self::_IMAP_HOST, $this->user, $this->password); |
173 | - | if (!$this->imapStream) throw new Exception("Cannot connect to " . self::_IMAP_HOST); |
173 | + | if (!$this->imapStream) throw new Exception("Cannot connect to " . self::_IMAP_HOST); |
174 | - | } |
174 | + | } |
175 | - | |
175 | + | |
176 | - | private function fetch($msgNumber) |
176 | + | private function fetch($msgNumber) |
177 | - | { |
177 | + | { |
178 | - | return imap_fetchbody($this->imapStream, $msgNumber, "2", FT_UID); |
178 | + | return imap_fetchbody($this->imapStream, $msgNumber, "2", FT_UID); |
179 | - | } |
179 | + | } |
180 | } |