forked from iwatllc/CI_Imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MY_Email.php
437 lines (371 loc) · 16.8 KB
/
MY_Email.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* iWAT LLC Email Class
*
* Overide of CI Email class adding the features to recieve
* email.
*
* Used this as a resouce for building my class.
* https://github.com/barbushin/php-imap/blob/master/src/ImapMailbox.php
* https://github.com/barbushin/php-imap/blob/master/example/index.php
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author iWAT LLC
* @link http://example.com
*
* 1-13-2014 - Update for php-imap fix #46
* https://github.com/georaldc/php-imap/commit/04b0c19130a243d51269f008b52a735cfad1a82f
*
*/
class MY_Email extends CI_Email {
var $imap_host = ""; // IMAP Server. Example: mail.earthlink.net
var $imap_user = ""; // IMAP User.
var $imap_pass = ""; // IMAP Password.
var $imap_port = ""; // IMAP Port. Example: 993 for secure connection.
var $imap_mailbox = ""; // IMAP Mailbox. Example: INBOX
var $imap_path = ""; // IMAP Mailbox Path. Example: 'imap/ssl'
var $imap_server_encoding = ""; // IMAP server encoding
var $imap_attachemnt_dir = ""; // IMAP Attachemts directory
/**
* Constructor - Sets IMAP Email Preferences
*
* The constructor can be passed an array of config values
*/
public function __construct($config = array())
{
parent::__construct($config);
}
// --------------------------------------------------------------------
/**
* Get IMAP mailbox connection stream
* @param bool $forceConnection Initialize connection if it's not initialized
* @return null|resource
*/
public function get_imap_stream($force_connection = true) {
static $imap_stream;
if($force_connection) {
if($imap_stream && (!is_resource($imap_stream) || !imap_ping($imap_stream))) {
$this->disconnect();
$imap_stream = null;
}
if(!$imap_stream) {
$imap_stream = $this->_init_imap_stream();
}
}
return $imap_stream;
}
// --------------------------------------------------------------------
protected function _init_imap_stream() {
$imap_stream = @imap_open($this->_full_imap_path(), $this->imap_user, $this->imap_pass);
if(!$imap_stream) {
throw new Exception('Connection error: ' . imap_last_error());
}
return $imap_stream;
}
// --------------------------------------------------------------------
protected function _full_imap_path() {
$full_imap_path = "{".$this->imap_host.":".$this->imap_port.$this->imap_path."}".$this->imap_mailbox;
return $full_imap_path;
}
/**
* Get information about the current mailbox.
*
* Returns the information in an object with following properties:
* Date - current system time formatted according to RFC2822
* Driver - protocol used to access this mailbox: POP3, IMAP, NNTP
* Mailbox - the mailbox name
* Nmsgs - number of mails in the mailbox
* Recent - number of recent mails in the mailbox
*
* @return stdClass
*/
public function check_mailbox() {
return imap_check($this->get_imap_stream());
}
// --------------------------------------------------------------------
/**
* Get information about the current mailbox.
*
* Returns an object with following properties:
* Date - last change (current datetime)
* Driver - driver
* Mailbox - name of the mailbox
* Nmsgs - number of messages
* Recent - number of recent messages
* Unread - number of unread messages
* Deleted - number of deleted messages
* Size - mailbox size
*
* @return object Object with info | FALSE on failure
*/
public function get_mailbox_info() {
return imap_mailboxmsginfo($this->get_imap_stream());
}
// --------------------------------------------------------------------
/**
* Get number of messages from the Mailbox.
*
* @return integer
*/
public function get_num_msg() {
return imap_num_msg($this->get_imap_stream());
}
// --------------------------------------------------------------------
/**
* Gets status information about the given mailbox.
*
* This function returns an object containing status information.
* The object has the following properties: messages, recent, unseen, uidnext, and uidvalidity.
*
* @return stdClass | FALSE if the box doesn't exist
*/
public function status_mailbox() {
return imap_status($this->get_imap_stream(), $this->imap_path, SA_ALL);
}
// --------------------------------------------------------------------
/**
* Close IMAP Connection
*
*/
protected function _disconnect() {
$imap_stream = $this->get_imap_stream(false);
if($imap_stream && is_resource($imap_stream)) {
imap_close($imap_stream, CL_EXPUNGE);
}
}
// --------------------------------------------------------------------
/**
* Marks mails listed in mailId for deletion.
* @return bool
*/
public function delete_mail($mail_id) {
return imap_delete($this->get_imap_stream(), $mail_id, FT_UID);
}
// --------------------------------------------------------------------
/**
* Deletes all the mails marked for deletion by imap_delete(), imap_mail_move(), or imap_setflag_full().
* @return bool
*/
public function expunge_deleted_mails() {
return imap_expunge($this->get_imap_stream());
}
// --------------------------------------------------------------------
/**
* This function performs a search on the mailbox currently opened in the given IMAP stream.
* For example, to match all unanswered mails sent by Mom, you'd use: "UNANSWERED FROM mom".
* Searches appear to be case insensitive. This list of criteria is from a reading of the UW
* c-client source code and may be incomplete or inaccurate (see also RFC2060, section 6.4.4).
*
* @param string $criteria String, delimited by spaces, in which the following keywords are allowed. Any multi-word arguments (e.g. FROM "joey smith") must be quoted. Results will match all criteria entries.
* ALL - return all mails matching the rest of the criteria
* ANSWERED - match mails with the \\ANSWERED flag set
* BCC "string" - match mails with "string" in the Bcc: field
* BEFORE "date" - match mails with Date: before "date"
* BODY "string" - match mails with "string" in the body of the mail
* CC "string" - match mails with "string" in the Cc: field
* DELETED - match deleted mails
* FLAGGED - match mails with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
* FROM "string" - match mails with "string" in the From: field
* KEYWORD "string" - match mails with "string" as a keyword
* NEW - match new mails
* OLD - match old mails
* ON "date" - match mails with Date: matching "date"
* RECENT - match mails with the \\RECENT flag set
* SEEN - match mails that have been read (the \\SEEN flag is set)
* SINCE "date" - match mails with Date: after "date"
* SUBJECT "string" - match mails with "string" in the Subject:
* TEXT "string" - match mails with text "string"
* TO "string" - match mails with "string" in the To:
* UNANSWERED - match mails that have not been answered
* UNDELETED - match mails that are not deleted
* UNFLAGGED - match mails that are not flagged
* UNKEYWORD "string" - match mails that do not have the keyword "string"
* UNSEEN - match mails which have not been read yet
*
* @return array Mails ids
*/
public function search_mailbox($criteria = 'ALL') {
$mails_ids = imap_search($this->get_imap_stream(), $criteria, SE_UID, $this->imap_server_encoding);
return $mails_ids ? $mails_ids : array();
}
// --------------------------------------------------------------------
/**
* Get mail data
*
* @param $mail_id
* @return IncomingMail
*/
public function get_mail($mail_id) {
$head = imap_rfc822_parse_headers(imap_fetchheader($this->get_imap_stream(), $mail_id, FT_UID));
$CI =& get_instance();
$CI->load->library('Incoming_Mail');
$incoming_mail = new $CI->incoming_mail();
$incoming_mail->id = $mail_id;
$incoming_mail->date = date('Y-m-d H:i:s', isset($head->date) ? strtotime($head->date) : time());
$incoming_mail->subject = isset($head->subject) ? $this->decode_mime_str($head->subject, $this->imap_server_encoding) : null;
$incoming_mail->from_name = isset($head->from[0]->personal) ? $this->decode_mime_str($head->from[0]->personal, $this->imap_server_encoding) : null;
$incoming_mail->from_address = strtolower($head->from[0]->mailbox . '@' . $head->from[0]->host);
if(isset($head->to)) {
$to_strings = array();
foreach($head->to as $to) {
if(!empty($to->mailbox) && !empty($to->host)) {
$to_email = strtolower($to->mailbox . '@' . $to->host);
$to_name = isset($to->personal) ? $this->decode_mime_str($to->personal, $this->imap_server_encoding) : null;
$to_strings[] = $to_name ? "$to_name <$to_email>" : $to_email;
$this->incoming_mail->to[$to_email] = $to_name;
}
}
$incoming_mail->to_string = implode(', ', $to_strings);
}
if(isset($head->cc)) {
foreach($head->cc as $cc) {
$incoming_mail->cc[strtolower($cc->mailbox . '@' . $cc->host)] = isset($cc->personal) ? $this->decode_mime_str($cc->personal, $this->imap_server_encoding) : null;
}
}
if(isset($head->reply_to)) {
foreach($head->reply_to as $reply_to) {
$incoming_mail->reply_to[strtolower($reply_to->mailbox . '@' . $reply_to->host)] = isset($reply_to->personal) ? $this->decode_mime_str($reply_to->personal, $this->imap_server_encoding) : null;
}
}
$mail_structure = imap_fetchstructure($this->get_imap_stream(), $mail_id, FT_UID);
if(empty($mail_structure->parts)) {
$this->_init_mail_part($incoming_mail, $mail_structure, 0);
}
else {
foreach($mail_structure->parts as $part_num => $part_structure) {
$this->_init_mail_part($incoming_mail, $part_structure, $part_num + 1);
}
}
return $incoming_mail;
}
// --------------------------------------------------------------------
protected function _init_mail_part($mail, $part_structure, $part_num) {
$data = $part_num ? imap_fetchbody($this->get_imap_stream(), $mail->id, $part_num, FT_UID) : imap_body($this->get_imap_stream(), $mail->id, FT_UID);
if($part_structure->encoding == 1) {
$data = imap_utf8($data);
}
elseif($part_structure->encoding == 2) {
$data = imap_binary($data);
}
elseif($part_structure->encoding == 3) {
$data = imap_base64($data);
}
elseif($part_structure->encoding == 4) {
$data = imap_qprint($data);
}
$params = array();
if(!empty($part_structure->parameters)) {
foreach($part_structure->parameters as $param) {
$params[strtolower($param->attribute)] = $param->value;
}
}
if(!empty($part_structure->dparameters)) {
foreach($part_structure->dparameters as $param) {
$paramName = strtolower(preg_match('~^(.*?)\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
if(isset($params[$paramName])) {
$params[$paramName] .= $param->value;
}
else {
$params[$paramName] = $param->value;
}
}
}
if(!empty($params['charset'])) {
// php-imap fix #46
//$data = iconv(strtoupper($params['charset']), $this->imap_server_encoding . '//IGNORE', $data);
$data = mb_convert_encoding($data, $this->imap_server_encoding, $params['charset']);
}
// attachments
$attachment_id = $part_structure->ifid
? trim($part_structure->id, " <>")
: (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
if($attachment_id) {
if(empty($params['filename']) && empty($params['name'])) {
$file_name = $attachment_id . '.' . strtolower($part_structure->subtype);
} else {
$file_name = !empty($params['filename']) ? $params['filename'] : $params['name'];
$file_name = $this->decode_mime_str($file_name, $this->imap_server_encoding);
$file_name = $this->decode_rfc2231($file_name, $this->imap_server_encoding);
}
$CI =& get_instance();
$CI->load->library('Incoming_Mail_Attachment');
$mailattachment = new $CI->incoming_mail_attachment();
$mailattachment->id = $attachment_id;
$mailattachment->name = $file_name;
if($this->imap_attachemnt_dir) {
$replace = array(
'/\s/' => '_',
'/[^0-9a-zA-Z_\.]/' => '',
'/_+/' => '_',
'/(^_)|(_$)/' => '',
);
$file_sys_name = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachment_id . '_' . preg_replace(array_keys($replace), $replace, $file_name));
$mailattachment->uploadfileName = $file_sys_name;
$mailattachment->filePath = $this->imap_attachemnt_dir . DIRECTORY_SEPARATOR . $file_sys_name;
file_put_contents($mailattachment->filePath, $data);
}
$mail->add_attachment($mailattachment);
unset($mailattachment);
} elseif($part_structure->type == 0 && $data) {
if(strtolower($part_structure->subtype) == 'plain') {
$mail->text_plain .= $data;
} else {
$mail->text_html .= $data;
}
} elseif($part_structure->type == 2 && $data) {
$mail->text_plain .= trim($data);
}
if(!empty($part_structure->parts)) {
foreach($part_structure->parts as $sub_part_num => $sub_part_structure) {
if($part_structure->type == 2 && $part_structure->subtype == 'RFC822') {
$this->_init_mail_part($mail, $sub_part_structure, $part_num);
} else {
$this->_init_mail_part($mail, $sub_part_structure, $part_num . '.' . ($sub_part_num + 1));
}
}
}
}
// --------------------------------------------------------------------
/**
* Decode Mime String
*
* @param $string, $charset
* @return String
*/
protected function decode_mime_str($string, $charset = 'utf-8') {
$new_string = '';
$elements = imap_mime_header_decode($string);
for($i = 0; $i < count($elements); $i++) {
if($elements[$i]->charset == 'default') {
$elements[$i]->charset = 'iso-8859-1';
}
// php-imap fix #46
// $new_string .= iconv(strtoupper($elements[$i]->charset), $charset . '//IGNORE', $elements[$i]->text);
$new_string .= mb_convert_encoding($elements[$i]->text, $charset, $elements[$i]->charset);
}
return $new_string;
}
// --------------------------------------------------------------------
/**
* Decode RFC String
*
* @param $string, $charset
* @return String
*/
protected function decode_rfc2231($string, $charset = 'utf-8') {
if(preg_match("/^(.*?)'.*?'(.*?)$/", $string, $matches)) {
$encoding = $matches[1];
$data = $matches[2];
if($this->isUrlEncoded($data)) {
// php-imap fix #46
// $string = iconv(strtoupper($encoding), $charset . '//IGNORE', urldecode($data));
$string = mb_convert_encoding(urldecode($data), $charset, $encoding);
}
}
return $string;
}
}
/* End of file MY_Email.php */
/* Location: ./system/application/libraries/MY_Email.php */