├── .github ├── ISSUE_TEMPLATE └── PULL_REQUEST_TEMPLATE ├── .gitignore ├── .styleci.yml ├── .travis.yml ├── ImapClient ├── AdapterForOutgoingMessage.php ├── Helper.php ├── ImapClient.php ├── ImapClientException.php ├── ImapConnect.php ├── IncomingMessage.php ├── IncomingMessageAttachment.php ├── OutgoingMessage.php ├── Section.php ├── SubtypeBody.php ├── TypeAttachments.php └── TypeBody.php ├── LICENSE ├── README.md ├── archive └── ImapClient.php ├── autoload.php ├── composer.json ├── docs ├── connecting.html ├── contributing.html ├── errordb.html ├── examples.html ├── gettingstarted.html ├── guide │ ├── connecting.md │ ├── contributing.md │ ├── errordb.md │ ├── examples.md │ ├── gettingstarted.md │ ├── incomingmessage.md │ ├── install.md │ ├── methods.md │ ├── outgoing.md │ └── usage.md ├── incomingmessage.html ├── index.html ├── installing.html ├── methods.html ├── outgoing.html ├── phpImapClient.zip └── usage.html ├── runtests.php └── tests ├── Check.php ├── Message.php ├── MessageInterface.php ├── MessagesPool.php ├── SimpleMessage.php ├── TestImapClient.php ├── TestMessage1.php ├── config_example.php └── template.php /.github/ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | ### Feature request or bug 2 | 3 | ### If a bug, what did you expect to happen? 4 | 5 | ### If a bug, what happened? 6 | 7 | ### If a bug, list steps to reproduce bugs. 8 | 9 | ### If a bug, did you do these steps? 10 | [] Download and use the lastest stable version 11 | [] See if the issue has already been reported 12 | [] Debug 13 | 14 | ### If a feature request, what do you want to be added or changed? 15 | 16 | ### If a feature request, is this feature already in a pull request? 17 | 18 | ### If a feature request, do you know anyone who can help? 19 | 20 | ### Side notes(Read then del this chunk) 21 | Please use pastebin for var_dumps. Not screenies or paste blobs! 22 | Be descriptive! 23 | Please fill out this _entire_ form! 24 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | ### Is the pull request based off the lastest version? 2 | 3 | ### What features have you added? 4 | 5 | ### What bugs did you fix? 6 | 7 | ### Is your code valid PSR-2? 8 | 9 | ### Have you properly documented your code? 10 | 11 | ### Has anything in your pull request already been fixed? 12 | 13 | ### Anything else? 14 | 15 | ### Optional things below 16 | [] Added your name to the credits 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Numerous always-ignore extensions 2 | *.diff 3 | *.err 4 | *.orig 5 | *.log 6 | *.rej 7 | *.swo 8 | *.swp 9 | *.vi 10 | *~ 11 | 12 | # OS or Editor folders 13 | .DS_Store 14 | Thumbs.db 15 | .cache 16 | .project 17 | .settings 18 | .tmproj 19 | *.esproj 20 | nbproject 21 | *.sublime-project 22 | *.sublime-workspace 23 | 24 | # Dreamweaver added files 25 | _notes 26 | dwsync.xml 27 | 28 | # Komodo 29 | *.komodoproject 30 | .komodotools 31 | 32 | # Eclipse for PHP 33 | .metadata 34 | bin/ 35 | tmp/ 36 | *.tmp 37 | *.bak 38 | *.swp 39 | *~.nib 40 | local.properties 41 | .settings/ 42 | .loadpath 43 | .recommenders 44 | .project 45 | 46 | #PhpStorm 47 | .idea 48 | 49 | #Tests 50 | tests/local-test.php 51 | tests/config.php -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: recommended 2 | 3 | enabled: 4 | - long_array_syntax 5 | - strict 6 | - unalign_double_arrow 7 | 8 | disabled: 9 | - align_double_arrow 10 | - short_array_syntax 11 | 12 | finder: 13 | exclude: 14 | - "tests" 15 | name: 16 | - "*.php" 17 | not-contains: 18 | - "config" 19 | path: 20 | - "ImapClient/" 21 | depth: 22 | - "< 3" 23 | 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.4' 4 | - '5.5' 5 | - '5.6' 6 | - '7.0' 7 | - '7.1' 8 | - hhvm 9 | - nightly 10 | matrix: 11 | fast_finish: false 12 | allow_failures: 13 | - php: hhvm 14 | script: 15 | - phpunit runtests.php 16 | -------------------------------------------------------------------------------- /ImapClient/AdapterForOutgoingMessage.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class AdapterForOutgoingMessage 16 | { 17 | /** 18 | * Connect config 19 | * 20 | * @var array 21 | */ 22 | private $config; 23 | 24 | /** 25 | * Options 26 | * 27 | * Array ['to'=>'', 'subject'=>'' and other] 28 | * 29 | * @var array 30 | */ 31 | private static $options; 32 | 33 | /** 34 | * Constructor 35 | * 36 | * Called when the class is made. 37 | * 38 | * @param array $connectConfig 39 | */ 40 | public function __construct(array $connectConfig) 41 | { 42 | $this->config = $connectConfig; 43 | } 44 | 45 | /** 46 | * Set the options of this class 47 | * 48 | * @param array $options 49 | * @return void 50 | */ 51 | public static function setOptions(array $options) 52 | { 53 | self::$options = $options; 54 | } 55 | 56 | /** 57 | * Send an email. Not implemented 58 | * 59 | * @throws ImapClientException 60 | */ 61 | public function send() 62 | { 63 | throw new ImapClientException('Not implemented'); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /ImapClient/Helper.php: -------------------------------------------------------------------------------- 1 | , sergey144010 14 | */ 15 | class Helper 16 | { 17 | const OUT_OBJECT = 'object'; 18 | const OUT_ARRAY = 'array'; 19 | 20 | /** 21 | * Preparing properties. 22 | * 23 | * Return object like this 24 | * ```php 25 | * $obj->to => 'to', 26 | * $obj->subject => 'subject', 27 | * $obj->message => null 28 | * # it is if incoming array not have 'message', like this ['subject'=>'val', 'to'=>'val'] 29 | * ``` 30 | * 31 | * @param array $arrayCurrentPropertiesAndValues available properties like only ['subject'=>'val', 'message'=>'val'] 32 | * @param array $arrayAllowedProperties all need properties [... 'to', 'subject', 'message' ...] 33 | * @param string $outType if Helper::OUT_OBJECT return object, if Helper::OUT_ARRAY return array. 34 | * 35 | * @return object|array 36 | */ 37 | public static function preparingProperties($arrayCurrentPropertiesAndValues, $arrayAllowedProperties, $outType = self::OUT_OBJECT) 38 | { 39 | if ($outType === self::OUT_ARRAY) { 40 | $outArray = array(); 41 | foreach ($arrayAllowedProperties as $property) { 42 | if (!isset($arrayCurrentPropertiesAndValues[$property])) { 43 | $outArray[$property] = null; 44 | } else { 45 | $outArray[$property] = $arrayCurrentPropertiesAndValues[$property]; 46 | } 47 | } 48 | 49 | return $outArray; 50 | } 51 | if ($outType === self::OUT_OBJECT) { 52 | $obj = new \stdClass(); 53 | foreach ($arrayAllowedProperties as $property) { 54 | if (!isset($arrayCurrentPropertiesAndValues[$property])) { 55 | $obj->$property = null; 56 | } else { 57 | $obj->$property = $arrayCurrentPropertiesAndValues[$property]; 58 | } 59 | } 60 | 61 | return $obj; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ImapClient/ImapClient.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class ImapClient 16 | { 17 | /** 18 | * Use the Secure Socket Layer to encrypt the session 19 | */ 20 | const ENCRYPT_SSL = 'ssl'; 21 | 22 | /** 23 | * Force use of start-TLS to encrypt the session, and reject connection to servers that do not support it 24 | */ 25 | const ENCRYPT_TLS = 'tls'; 26 | const CONNECT_ADVANCED = 'connectAdvanced'; 27 | const CONNECT_DEFAULT = 'connectDefault'; 28 | 29 | /** 30 | * Connect status or advanced or default 31 | * 32 | * @var string 33 | */ 34 | public static $connect; 35 | 36 | /** 37 | * Config for advanced connect 38 | * 39 | * @var array 40 | */ 41 | public static $connectConfig; 42 | 43 | /** 44 | * Incoming message 45 | * 46 | * @var IncomingMessage 47 | */ 48 | public $incomingMessage; 49 | 50 | /** 51 | * Imap connection 52 | * 53 | * @var resource ImapConnect 54 | */ 55 | protected $imap; 56 | 57 | /** 58 | * Mailbox url 59 | * 60 | * @var string 61 | */ 62 | protected $mailbox = ""; 63 | 64 | /** 65 | * Current folder 66 | * 67 | * @var string 68 | */ 69 | protected $folder = "INBOX"; 70 | 71 | /** 72 | * Initialize imap helper 73 | * 74 | * @param string $mailbox 75 | * @param string $username 76 | * @param string $password 77 | * @param string $encryption use ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS 78 | */ 79 | public function __construct($mailbox = null, $username = null, $password = null, $encryption = null) 80 | { 81 | if(isset($mailbox) && is_string($mailbox)){ 82 | $this->setConnectDefault(); 83 | }; 84 | if(isset($mailbox) && is_array($mailbox)){ 85 | $this->setConnectAdvanced(); 86 | $this->setConnectConfig($mailbox); 87 | }; 88 | 89 | if(!isset(self::$connect) || self::$connect === self::CONNECT_DEFAULT){ 90 | $this->connectDefault($mailbox, $username, $password, $encryption); 91 | }; 92 | if(self::$connect === self::CONNECT_ADVANCED){ 93 | $this->connectAdvanced(self::$connectConfig); 94 | }; 95 | } 96 | 97 | /** 98 | * Get the imap resource 99 | * 100 | * @return resource 101 | */ 102 | public function getImap() 103 | { 104 | return $this->imap; 105 | } 106 | 107 | /** 108 | * Set connection to advanced 109 | * 110 | * @return void 111 | */ 112 | public static function setConnectAdvanced() 113 | { 114 | static::$connect = self::CONNECT_ADVANCED; 115 | } 116 | 117 | /** 118 | * Set connection to default 119 | * 120 | * @return void 121 | */ 122 | public static function setConnectDefault() 123 | { 124 | static::$connect = self::CONNECT_DEFAULT; 125 | } 126 | 127 | /** 128 | * Get the imap connection 129 | * 130 | * $return imap 131 | */ 132 | public function getImapConnection() { 133 | return $this->imap; 134 | } 135 | 136 | /** 137 | * Set connection config 138 | * 139 | * @param array $config 140 | * @return void 141 | */ 142 | public static function setConnectConfig(array $config) 143 | { 144 | static::$connectConfig = $config; 145 | } 146 | 147 | /** 148 | * The default connection. 149 | * Not used a lot of imap connection options. 150 | * Use only ENCRYPT_SSL and VALIDATE_CERT. 151 | * 152 | * If you need a more advanced connection settings, 153 | * use connectAdvanced() method. 154 | * 155 | * @param string $mailbox 156 | * @param string $username 157 | * @param string $password 158 | * @param string $encryption use ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS 159 | * @return void 160 | */ 161 | public function connectDefault($mailbox, $username, $password, $encryption = null) 162 | { 163 | $connect = new ImapConnect(); 164 | if($encryption === ImapClient::ENCRYPT_SSL){ 165 | $connect->prepareFlags(ImapConnect::SERVICE_IMAP, ImapConnect::ENCRYPT_SSL, ImapConnect::NOVALIDATE_CERT); 166 | }; 167 | if($encryption === ImapClient::ENCRYPT_TLS){ 168 | $connect->prepareFlags(ImapConnect::SERVICE_IMAP, ImapConnect::ENCRYPT_TLS, ImapConnect::NOVALIDATE_CERT); 169 | }; 170 | $connect->prepareMailbox($mailbox); 171 | $connect->connect(null, $username, $password); 172 | $this->imap = $connect->getImap(); 173 | $this->mailbox = $connect->getMailbox(); 174 | } 175 | 176 | /** 177 | * Advanced connect 178 | * 179 | * @param array $config 180 | * @return void 181 | * @throws ImapClientException 182 | */ 183 | public function connectAdvanced(array $config) 184 | { 185 | if(!isset($config['flags'])){$config['flags'] = null;}; 186 | if(!isset($config['mailbox'])){$config['mailbox'] = null;}; 187 | if(!isset($config['connect'])){ 188 | throw new ImapClientException('Option connect must be installed'); 189 | }; 190 | $connect = new ImapConnect(); 191 | $connect->prepareFlags($config['flags']); 192 | $connect->prepareMailbox($config['mailbox']); 193 | $connect->connect($config['connect']); 194 | $this->imap = $connect->getImap(); 195 | $this->mailbox = $connect->getMailbox(); 196 | } 197 | 198 | /** 199 | * Close connection 200 | * 201 | * Also called during garbage collection 202 | * 203 | * @return void 204 | */ 205 | public function __destruct() 206 | { 207 | if (is_resource($this->imap)) 208 | { 209 | imap_close($this->imap); 210 | } 211 | } 212 | 213 | /** 214 | * Returns true after successful connection 215 | * 216 | * @return bool true on success 217 | */ 218 | public function isConnected() 219 | { 220 | return $this->imap !== false; 221 | } 222 | 223 | /** 224 | * Saves the email into a file 225 | * Note: If your server does not have alot of RAM, this may break 226 | * 227 | * @param string $file 228 | * @param integer $id 229 | * @param string $part 230 | * @return bool true on success 231 | * @throws ImapClientException 232 | */ 233 | public function saveEmail($file = null, $id = null, $part = null) 234 | { 235 | // Null checks 236 | if($file === null) 237 | { 238 | throw new ImapClientException('File must be specified for saveEmail()'); 239 | } 240 | if($id === null) 241 | { 242 | throw new ImapClientException('Email id must be specified for saveEmail()'); 243 | } 244 | if($part === null) 245 | { 246 | $parts = false; 247 | } 248 | else { 249 | $parts = true; 250 | } 251 | // Is type checks 252 | if(!is_string($file)) 253 | { 254 | throw new ImapClientException('File must be a string for saveEmail()'); 255 | } 256 | if(!is_int($id)) 257 | { 258 | throw new ImapClientException('$id must be a integer for saveEmail()'); 259 | } 260 | $saveFile = fopen($file,'w'); 261 | if($parts) 262 | { 263 | $ret = imap_savebody($this->imap, $saveFile, $id, $part); 264 | } 265 | else { 266 | $ret = imap_savebody($this->imap, $saveFile, $id); 267 | } 268 | fclose($saveFile); 269 | 270 | return $ret; 271 | } 272 | 273 | /** 274 | * Saves the email into a file 275 | * Note: This is safer then saveEmail for slower servers 276 | * 277 | * @param string $file 278 | * @param integer $id 279 | * @param string $part 280 | * @param string $streamFilter 281 | * @return bool true on success 282 | * @throws ImapClientException 283 | */ 284 | public function saveEmailSafe($file = null, $id = null, $part = null, $streamFilter = 'convert.base64-decode') 285 | { 286 | // Null checks 287 | if($file === null) 288 | { 289 | throw new ImapClientException('File must be specified for saveEmailSafe()'); 290 | } 291 | if($id === null) 292 | { 293 | throw new ImapClientException('Email id must be specified for saveEmailSafe()'); 294 | } 295 | if($part === null) 296 | { 297 | $parts = false; 298 | } 299 | else { 300 | $parts = true; 301 | } 302 | // Is type checks 303 | if(!is_string($file)) 304 | { 305 | throw new ImapClientException('File must be a string for saveEmailSafe()'); 306 | } 307 | if(!is_int($id)) 308 | { 309 | throw new ImapClientException('$id must be a integer for saveEmailSafe()'); 310 | } 311 | $saveFile = fopen($file,'w'); 312 | stream_filter_append($saveFile, $streamFilter, STREAM_FILTER_WRITE); 313 | if($parts) 314 | { 315 | $ret = imap_savebody($this->imap, $saveFile, $id, $part); 316 | } 317 | else { 318 | $ret = imap_savebody($this->imap, $saveFile, $id); 319 | }; 320 | fclose($saveFile); 321 | 322 | return $ret; 323 | } 324 | 325 | /** 326 | * Returns the last imap error 327 | * 328 | * @return string error message 329 | */ 330 | public function getError() 331 | { 332 | return imap_last_error(); 333 | } 334 | 335 | /** 336 | * Select the given folder folder 337 | * and set $this->folder 338 | * 339 | * @param string $folder name 340 | * @return bool successful opened folder 341 | */ 342 | public function selectFolder($folder) 343 | { 344 | $result = imap_reopen($this->imap, $this->mailbox . $folder); 345 | if ($result === true) { 346 | $this->folder = $folder; 347 | } 348 | return $result; 349 | } 350 | 351 | /** 352 | * Returns all available folders 353 | * 354 | * @param string $separator default is '.' 355 | * @param integer $type has three meanings 0,1,2. 356 | * If 0 return nested array, if 1 return an array of strings, if 2 return raw imap_list() 357 | * @return array with folder names 358 | */ 359 | public function getFolders($separator = null, $type = 0) 360 | { 361 | if(preg_match( '/^{.+}/', $this->mailbox, $matches)){ 362 | $mailbox = $matches[0]; 363 | }else{ 364 | $mailbox = $this->mailbox; 365 | }; 366 | 367 | $folders = imap_list($this->imap, $mailbox, "*"); 368 | 369 | if ($type == 2) { 370 | return $folders; 371 | }; 372 | if ($type == 1) { 373 | return str_replace($mailbox, "", $folders); 374 | }; 375 | if ($type == 0) { 376 | $arrayRaw = str_replace($mailbox, "", $folders); 377 | if (!isset($separator)) { 378 | $separator = '.'; 379 | }; 380 | $arrayNew = []; 381 | foreach ($arrayRaw as $string) { 382 | $array = explode($separator, $string); 383 | $count = count($array); 384 | $count = $count-1; 385 | $cache = false; 386 | for($i=$count; $i>=0; $i--){ 387 | if($i == $count){ 388 | $cache = [$array[$i]=>[]]; 389 | }else{ 390 | $cache = [$array[$i] => $cache]; 391 | }; 392 | }; 393 | $arrayNew = array_merge_recursive($arrayNew, $cache); 394 | }; 395 | return $arrayNew; 396 | } 397 | return null; 398 | } 399 | 400 | /** 401 | * Returns the number of messages in the current folder 402 | * 403 | * @return int message count 404 | */ 405 | public function countMessages() 406 | { 407 | return imap_num_msg($this->imap); 408 | } 409 | 410 | /** 411 | * Returns an array of brief information about each message in the current mailbox. 412 | * 413 | * Returns the following structure of the array of arrays. 414 | * ```php 415 | * $array = [ 416 | * [ 'id'=>4, 'info'=>'brief info' ] 417 | * [ 'id'=>5, 'info'=>'brief info' ] 418 | * ] 419 | *``` 420 | * @return array 421 | */ 422 | public function getBriefInfoMessages() 423 | { 424 | $array = imap_headers($this->imap); 425 | $newArray = []; 426 | foreach ($array as $key => $string) { 427 | $newArray[] = ['id'=>$key+1, 'info' => $string]; 428 | }; 429 | return $newArray; 430 | } 431 | 432 | /** 433 | * Returns the number of unread messages in the current folder 434 | * 435 | * @return integer Message count 436 | */ 437 | public function countUnreadMessages() { 438 | $result = imap_search($this->imap, 'UNSEEN'); 439 | if ($result === false) { 440 | return 0; 441 | } 442 | return count($result); 443 | } 444 | 445 | /** 446 | * Returns unseen emails in the current folder 447 | * 448 | * @param bool $read Mark message like SEEN or no. 449 | * @return array objects IncomingMessage 450 | * @throws ImapClientException 451 | */ 452 | public function getUnreadMessages($read = true) { 453 | $emails = []; 454 | $result = imap_search($this->imap, 'UNSEEN'); 455 | if(!$result){ 456 | throw new ImapClientException('No read messages were found.'); 457 | }; 458 | $ids = ''; $countId = count($result); 459 | foreach($result as $key=>$id) { 460 | $emails[]= $this->getMessage($id); 461 | if(($countId-1) == $key){ 462 | $ids .= $id; 463 | }else{ 464 | $ids .= $id.','; 465 | }; 466 | } 467 | /* Set flag UNSEEN */ 468 | if(!$read){ 469 | $this->setUnseenMessage($ids); 470 | }else{ 471 | $this->setSeenMessage($ids); 472 | }; 473 | return $emails; 474 | } 475 | 476 | /** 477 | * Get Messages by Criteria 478 | * 479 | * @see http://php.net/manual/en/function.imap-search.php 480 | * @param string $criteria ALL, UNSEEN, FLAGGED, UNANSWERED, DELETED, UNDELETED, etc (e.g. FROM "joey smith") 481 | * @param int $number 482 | * @param int $start 483 | * @param string $order 484 | * @return array 485 | * @throws ImapClientException 486 | */ 487 | public function getMessagesByCriteria($criteria = '', $number = 0, $start = 0, $order = 'DESC') 488 | { 489 | $emails = array(); 490 | $result = imap_search($this->imap, $criteria); 491 | if(!$result){ 492 | throw new ImapClientException('Messages not found. Or this criteria not supported on your email server.'); 493 | }; 494 | if ($number == 0) 495 | { 496 | $number = count($result); 497 | } 498 | if ($result) 499 | { 500 | $ids = array(); 501 | foreach ($result as $k => $i) 502 | { 503 | $ids[] = $i; 504 | } 505 | $ids = array_chunk($ids, $number); 506 | $ids = array_slice($ids[0], $start, $number); 507 | 508 | $emails = array(); 509 | foreach ($ids as $id) 510 | { 511 | $emails[] = $this->getMessage($id); 512 | } 513 | } 514 | if ($order == 'DESC') 515 | { 516 | $emails = array_reverse($emails); 517 | } 518 | 519 | return $emails; 520 | } 521 | 522 | /** 523 | * Save Attachments Messages By Subject 524 | * 525 | * @param string $subject 526 | * @param string $dir for save attachments 527 | * @param string $charset for search 528 | * @return void 529 | * @throws ImapClientException 530 | */ 531 | public function saveAttachmentsMessagesBySubject($subject, $dir = null, $charset = null) 532 | { 533 | $criteria = 'SUBJECT "'.$subject.'"'; 534 | $ids = imap_search($this->imap, $criteria, null, $charset); 535 | if(!$ids){ 536 | throw new ImapClientException('Messages not found. Or this criteria not supported on your email server.'); 537 | }; 538 | foreach ($ids as $id) { 539 | $this->getMessage($id); 540 | if(isset($dir)){ 541 | $dir = ['dir'=>$dir]; 542 | }; 543 | $this->saveAttachments($dir); 544 | }; 545 | } 546 | 547 | /** 548 | * Get messages 549 | * 550 | * @param int $number Number of messages. 0 to get all 551 | * @param int $start Starting message number 552 | * @param string $order ASC or DESC 553 | * @return array IncomingMessage of objects 554 | */ 555 | public function getMessages($number = 0, $start = 0, $order = 'DESC') 556 | { 557 | if ($number == 0) 558 | { 559 | $number = $this->countMessages(); 560 | } 561 | $emails = array(); 562 | $result = imap_search($this->imap, 'ALL'); 563 | if ($result) 564 | { 565 | $ids = array(); 566 | foreach ($result as $k => $i) 567 | { 568 | $ids[] = $i; 569 | } 570 | 571 | if ($order == 'DESC') 572 | { 573 | $ids = array_reverse($ids); 574 | } 575 | 576 | if (count($ids) > $number) { 577 | $ids = array_chunk($ids, $number); 578 | $ids = $ids[$start]; 579 | } 580 | 581 | if (isset($ids)) { 582 | foreach ($ids as $id) { 583 | $emails[] = $this->getMessage($id); 584 | } 585 | } 586 | } 587 | return $emails; 588 | } 589 | 590 | /** 591 | * Returns one email by given id 592 | * 593 | * Examples: 594 | * 595 | * 1. Structure 596 | * ```php 597 | * $imap = new ImapClient(); 598 | * $imap->getMessage(5); 599 | * ``` 600 | * 601 | * You can see all structure that 602 | * ```php 603 | * var_dump($imap->incomingMessage) 604 | * ``` 605 | * 606 | * But use like this 607 | * ```php 608 | * $imap->incomingMessage->header->subject 609 | * $imap->incomingMessage->header->from 610 | * $imap->incomingMessage->header->to 611 | * # cc or bcc 612 | * $imap->incomingMessage->header->details->cc 613 | * $imap->incomingMessage->header->details->bcc 614 | * # and other ... 615 | * var_dump($imap->incomingMessage->header) 616 | * ``` 617 | * 618 | * Next Text or Html body 619 | * ```php 620 | * $imap->incomingMessage->message->html 621 | * $imap->incomingMessage->message->plain 622 | * # below is array 623 | * $imap->incomingMessage->message->info 624 | * ``` 625 | * 626 | * Array attachments 627 | * ```php 628 | * $imap->incomingMessage->attachment 629 | * ``` 630 | * Attachment have structure and body 631 | * ```php 632 | * $imap->incomingMessage->attachment[0] 633 | * $imap->incomingMessage->attachment[0]->structure 634 | * $imap->incomingMessage->attachment[0]->body 635 | * ``` 636 | * 637 | * Count section 638 | * ```php 639 | * $imap->incomingMessage->section 640 | * ``` 641 | * 642 | * And structure all message 643 | * ```php 644 | * $imap->incomingMessage->structure 645 | * ``` 646 | * 647 | * 2. Save all attachments 648 | * ```php 649 | * $imap->getMessage(5); 650 | * $imap->saveAttachments(); 651 | * ``` 652 | * @see IncomingMessage 653 | * @param integer $id 654 | * @param string $decode IncomingMessage::DECODE or IncomingMessage::NOT_DECODE 655 | * @return object IncomingMessage 656 | */ 657 | public function getMessage($id, $decode = IncomingMessage::DECODE) 658 | { 659 | $this->checkMessageId($id); 660 | $this->incomingMessage = new IncomingMessage($this->imap, $id, $decode); 661 | return $this->incomingMessage; 662 | } 663 | 664 | /** 665 | * Get a section of the message 666 | * 667 | * @param integer $id 668 | * @param string $section 669 | * @return object 670 | */ 671 | public function getSection($id, $section) 672 | { 673 | $incomingMessage = new IncomingMessage($this->imap, $id); 674 | return $incomingMessage->getSection($section); 675 | } 676 | 677 | /** 678 | * Save attachments one incoming message 679 | * 680 | * The allowed types are TypeAttachments 681 | * You can add your own 682 | * 683 | * @param array $options have next parameters 684 | * ```php 685 | * # it is directory for save attachments 686 | * $options['dir'] 687 | * # it is incomingMessage object 688 | * $options['incomingMessage'] 689 | * ``` 690 | * @return void 691 | */ 692 | public function saveAttachments($options = null) 693 | { 694 | if(!isset($options['dir'])){ 695 | $dir = __DIR__.DIRECTORY_SEPARATOR; 696 | }else{ 697 | $dir = $options['dir']; 698 | }; 699 | if(!isset($options['incomingMessage'])){ 700 | $incomingMessage = $this->incomingMessage; 701 | }else{ 702 | $incomingMessage = $options['incomingMessage']; 703 | }; 704 | foreach ($incomingMessage->attachments as $key => $attachment) { 705 | $newFileName = $attachment->name; 706 | file_put_contents($dir.DIRECTORY_SEPARATOR.$newFileName, $attachment->body); 707 | }; 708 | } 709 | 710 | /** 711 | * Delete the given message 712 | * 713 | * @param int $id of the message 714 | * @return bool success or not 715 | */ 716 | public function deleteMessage($id) 717 | { 718 | return $this->deleteMessages(array($id)); 719 | } 720 | 721 | /** 722 | * Delete messages 723 | * 724 | * @return bool success or not 725 | * @param $ids array of ids 726 | */ 727 | public function deleteMessages($ids) 728 | { 729 | foreach ($ids as $id) { 730 | imap_delete($this->imap, $id, FT_UID); 731 | }; 732 | return imap_expunge($this->imap); 733 | } 734 | 735 | /** 736 | * Move given message in new folder 737 | * 738 | * @param int $id of the message 739 | * @param string $target new folder 740 | * @return bool success or not 741 | */ 742 | public function moveMessage($id, $target) 743 | { 744 | return $this->moveMessages(array($id), $target); 745 | } 746 | 747 | /** 748 | * Move given message in new folder 749 | * 750 | * @param array $ids array of message ids 751 | * @param string $target new folder 752 | * @return bool success or not 753 | */ 754 | public function moveMessages($ids, $target) 755 | { 756 | if (imap_mail_move($this->imap, implode(",", $ids), $target, CP_UID) === false) 757 | return false; 758 | return imap_expunge($this->imap); 759 | } 760 | 761 | /** 762 | * Set flag message SEEN 763 | * 764 | * @param int $ids or string like 1,2,3,4,5 or string like 1:5 765 | * @return bool 766 | */ 767 | public function setSeenMessage($ids) 768 | { 769 | // We need better docs for this 770 | return imap_setflag_full($this->imap, $ids, "\\Seen"); 771 | } 772 | 773 | /** 774 | * Delete flag message SEEN 775 | * 776 | * @param int $ids or string like 1,2,3,4,5 or string like 1:5 777 | * @return bool 778 | */ 779 | public function setUnseenMessage($ids) 780 | { 781 | // We need better docs for this 782 | return imap_clearflag_full($this->imap, $ids, "\\Seen"); 783 | } 784 | 785 | /** 786 | * Add a new folder 787 | * 788 | * @param string $name of the folder 789 | * @param bool|false $subscribe immediately subscribe to folder 790 | * @return bool success or not 791 | */ 792 | public function addFolder($name, $subscribe = false) 793 | { 794 | $success = imap_createmailbox($this->imap, $this->mailbox . $name); 795 | 796 | if ($success && $subscribe) { 797 | $success = imap_subscribe($this->imap, $this->mailbox . $name); 798 | } 799 | 800 | return $success; 801 | } 802 | 803 | /** 804 | * Remove a folder 805 | * 806 | * @param string $name of the folder 807 | * @return bool success or not 808 | */ 809 | public function removeFolder($name) 810 | { 811 | return imap_deletemailbox($this->imap, $this->mailbox . $name); 812 | } 813 | 814 | /** 815 | * Rename a folder 816 | * 817 | * @param string $name of the folder 818 | * @param string $newname of the folder 819 | * @return bool success or not 820 | */ 821 | public function renameFolder($name, $newname) 822 | { 823 | return imap_renamemailbox($this->imap, $this->mailbox . $name, $this->mailbox . $newname); 824 | } 825 | 826 | /** 827 | * Clean up trash AND spam folder 828 | * 829 | * @return bool success or not 830 | */ 831 | public function purge() 832 | { 833 | // delete trash and spam 834 | if ($this->folder==$this->getTrash() || strtolower($this->folder)=="spam") { 835 | if (imap_delete($this->imap,'1:*') === false) { 836 | return false; 837 | } 838 | return imap_expunge($this->imap); 839 | 840 | // move others to trash 841 | } else { 842 | if (imap_mail_move($this->imap,'1:*', $this->getTrash()) == false) { 843 | return false; 844 | } 845 | return imap_expunge($this->imap); 846 | } 847 | } 848 | 849 | /** 850 | * Returns all email addresses in all folders 851 | * 852 | * If you have a lot of folders and letters, it can take a long time. 853 | * And mark all the letters as read. 854 | * 855 | * @param array|null $options have options: 856 | * ```php 857 | * $options['getFolders']['separator'] 858 | * $options['getFolders']['type'] 859 | * $options['mark'] = SEEN and $options['mark'] = UNSEEN 860 | * ``` 861 | * @return array with all email addresses or false on error 862 | */ 863 | public function getAllEmailAddresses(array $options = null) 864 | { 865 | /* Check Options */ 866 | if(!isset($options['getFolders']['separator'])){ 867 | $options['getFolders']['separator'] = '.'; 868 | }; 869 | if(!isset($options['getFolders']['type'])){ 870 | $options['getFolders']['type'] = 1; 871 | }; 872 | if(!isset($options['mark'])){ 873 | $options['mark'] = 'SEEN'; 874 | }; 875 | 876 | $saveCurrentFolder = $this->folder; 877 | $emails = array(); 878 | foreach($this->getFolders($options['getFolders']['separator'], $options['getFolders']['type']) as $folder) { 879 | $this->selectFolder($folder); 880 | /** 881 | * @var $message IncomingMessage 882 | */ 883 | foreach($this->getMessages() as $message) { 884 | $emails[] = $message->header->from; 885 | $emails = array_merge($emails, $message->header->to); 886 | if (isset($message->header->details->cc)) { 887 | $emails = array_merge($emails, $message->header->details->cc); 888 | }; 889 | if(isset($options['mark']) && $options['mark'] == 'UNSEEN'){ 890 | $this->setUnseenMessage($message->header->msgno); 891 | }; 892 | } 893 | } 894 | $this->selectFolder($saveCurrentFolder); 895 | return array_unique($emails); 896 | } 897 | 898 | /** 899 | * Returns email addresses in the specified folder 900 | * 901 | * @param string $folder Specified folder 902 | * @param array|null $options have option 903 | * ```php 904 | * $options['mark'] = SEEN and $options['mark'] = UNSEEN 905 | * ``` 906 | * @return array addresses 907 | */ 908 | public function getEmailAddressesInFolder($folder, array $options = null) 909 | { 910 | if(!isset($options['mark'])){ 911 | $options['mark'] = 'SEEN'; 912 | }; 913 | $saveCurrentFolder = $this->folder; 914 | $this->selectFolder($folder); 915 | $emails = array(); 916 | /** 917 | * @var $message IncomingMessage 918 | */ 919 | foreach($this->getMessages() as $message) { 920 | $emails[] = $message->header->from; 921 | $emails = array_merge($emails, $message->header->to); 922 | if (isset($message->header->details->cc)) { 923 | $emails = array_merge($emails, $message->header->details->cc); 924 | }; 925 | if(isset($options['mark']) && $options['mark'] == 'UNSEEN'){ 926 | $this->setUnseenMessage($message->header->msgno); 927 | }; 928 | }; 929 | $this->selectFolder($saveCurrentFolder); 930 | return array_unique($emails); 931 | } 932 | 933 | /** 934 | * Save email in sent 935 | * 936 | * @param string $header 937 | * @param string $body 938 | * @return bool 939 | */ 940 | public function saveMessageInSent($header, $body) { 941 | return imap_append($this->imap, $this->mailbox . $this->getSent(), $header . "\r\n" . $body . "\r\n", "\\Seen"); 942 | } 943 | 944 | /** 945 | * Explicitly close imap connection 946 | */ 947 | public function close() { 948 | if ($this->imap !== false) { 949 | imap_close($this->imap); 950 | } 951 | } 952 | 953 | /** 954 | * Get trash folder 955 | * 956 | * @return string trash folder name 957 | */ 958 | protected function getTrash() 959 | { 960 | foreach ($this->getFolders(null, 1) as $folder) { 961 | if (in_array(strtolower($folder), array('trash', 'inbox.trash', 'papierkorb'))) { 962 | return $folder; 963 | } 964 | } 965 | 966 | // no trash folder found? create one 967 | $this->addFolder('Trash'); 968 | return 'Trash'; 969 | } 970 | 971 | /** 972 | * Get sent 973 | * 974 | * @return string sent folder name 975 | */ 976 | protected function getSent() 977 | { 978 | foreach ($this->getFolders(null, 1) as $folder) { 979 | if (in_array(strtolower($folder), array('sent', 'gesendet', 'inbox.gesendet'))) { 980 | return $folder; 981 | } 982 | } 983 | 984 | // no sent folder found? create one 985 | $this->addFolder('Sent'); 986 | return 'Sent'; 987 | } 988 | 989 | /** 990 | * Fetch message by id 991 | * 992 | * @param integer $id of the message 993 | * @return object|false header 994 | */ 995 | public function getMessageHeader($id) 996 | { 997 | return $this->imapHeaderInfo($id); 998 | } 999 | 1000 | /** 1001 | * Get message overview 1002 | * 1003 | * @see ImapClient::imapFetchOverview() 1004 | * @param integer $id 1005 | * @param null $options 1006 | * @return object 1007 | */ 1008 | public function getMessageOverview($id, $options = null) 1009 | { 1010 | $array = $this->imapFetchOverview($id, $options); 1011 | return $array[0]; 1012 | } 1013 | 1014 | /** 1015 | * Get messages overview 1016 | * 1017 | * @param string $id 1018 | * @param null $options 1019 | * @return array 1020 | */ 1021 | public function getMessagesOverview($id, $options = null) 1022 | { 1023 | return $this->imapFetchOverview($id, $options); 1024 | } 1025 | 1026 | /** 1027 | * Wrapper for php imap_fetch_overview() 1028 | * 1029 | * @see http://php.net/manual/ru/function.imap-fetch-overview.php 1030 | * @param string $sequence a message sequence description, 1031 | * you can enumerate desired messages with the X,Y syntax, 1032 | * or retrieve all messages within an interval with the X:Y syntax 1033 | * @param int $options sequence will contain a sequence of message indices or UIDs, 1034 | * if this parameter is set to FT_UID. 1035 | * @return array 1036 | */ 1037 | public function imapFetchOverview($sequence, $options = null) 1038 | { 1039 | return imap_fetch_overview($this->imap, $sequence, $options); 1040 | } 1041 | 1042 | /** 1043 | * Wrapper for php imap_headerinfo() 1044 | * 1045 | * @see http://php.net/manual/ru/function.imap-headerinfo.php 1046 | * @see http://php.net/manual/en/function.imap-headerinfo.php#98809 1047 | * @param integer $id 1048 | * @return object|false 1049 | */ 1050 | public function imapHeaderInfo($id) 1051 | { 1052 | return imap_rfc822_parse_headers(imap_fetchheader($this->imap, $id)); 1053 | } 1054 | 1055 | /** 1056 | * Wrapper for imap_fetchstructure() 1057 | * 1058 | * @see http://php.net/manual/ru/function.imap-fetchstructure.php 1059 | * @param integer $id 1060 | * @return object 1061 | */ 1062 | public function imapFetchStructure($id) 1063 | { 1064 | return imap_fetchstructure($this->imap, $id); 1065 | } 1066 | 1067 | /** 1068 | * Convert imap given address into string 1069 | * 1070 | * @param object $headerinfos the infos given by imap 1071 | * @return string in format "Name " 1072 | */ 1073 | protected function toAddress($headerinfos) { 1074 | $email = ""; 1075 | $name = ""; 1076 | if (isset($headerinfos->mailbox) && isset($headerinfos->host)) { 1077 | $email = $headerinfos->mailbox . "@" . $headerinfos->host; 1078 | } 1079 | 1080 | if (!empty($headerinfos->personal)) { 1081 | $name = imap_mime_header_decode($headerinfos->personal); 1082 | $name = $name[0]->text; 1083 | } else { 1084 | $name = $email; 1085 | } 1086 | 1087 | $name = $this->convertToUtf8($name); 1088 | 1089 | return $name . " <" . $email . ">"; 1090 | } 1091 | 1092 | /** 1093 | * Converts imap given array of addresses as strings 1094 | * 1095 | * @param array $addresses imap given addresses as array 1096 | * @return array with strings (e.g. ["Name ", "Name2 "] 1097 | */ 1098 | protected function arrayToAddress($addresses) { 1099 | $addressesAsString = array(); 1100 | foreach ($addresses as $address) { 1101 | $addressesAsString[] = $this->toAddress($address); 1102 | } 1103 | return $addressesAsString; 1104 | } 1105 | 1106 | /** 1107 | * Convert to utf8 if necessary. 1108 | * 1109 | * @param string $str utf8 encoded string 1110 | * @return bool 1111 | */ 1112 | public function convertToUtf8($str) { 1113 | if (mb_detect_encoding($str, "UTF-8, ISO-8859-1, GBK")!="UTF-8") { 1114 | $str = utf8_encode($str); 1115 | } 1116 | $str = iconv('UTF-8', 'UTF-8//IGNORE', $str); 1117 | return $str; 1118 | } 1119 | 1120 | /** 1121 | * Identify encoding by charset attribute in header 1122 | * 1123 | * @param $id 1124 | * @return string 1125 | */ 1126 | protected function getEncoding($id) 1127 | { 1128 | $header = $this->imapFetchStructure($id); 1129 | $params = $header->parameters ?: []; 1130 | foreach ($params as $k => $v) { 1131 | if (stristr($v->attribute, 'charset')) { 1132 | return $v->value; 1133 | } 1134 | } 1135 | return 'utf-8'; 1136 | } 1137 | 1138 | /** 1139 | * Return general mailbox statistics 1140 | * 1141 | * @return bool|resource object 1142 | */ 1143 | public function getMailboxStatistics() 1144 | { 1145 | return $this->isConnected() ? imap_mailboxmsginfo($this->imap) : false ; 1146 | } 1147 | 1148 | /** 1149 | * Unsubscribe from a mail box 1150 | * @return bool 1151 | */ 1152 | public function unSubscribe() 1153 | { 1154 | if (imap_unsubscribe($this->imap, $this->mailbox)) { 1155 | return true; 1156 | } 1157 | else { 1158 | return false; 1159 | } 1160 | } 1161 | 1162 | /** 1163 | * Retrieve the quota level settings, and usage statics per mailbox. 1164 | * 1165 | * @param string $mailbox 1166 | * 1167 | * @return array 1168 | */ 1169 | public function getQuota($mailbox) 1170 | { 1171 | $quota = imap_get_quota($this->imap, "user.".$mailbox); 1172 | return $quota; 1173 | } 1174 | 1175 | /** 1176 | * Retrieve the quota level settings, and usage statics per mailbox. 1177 | * 1178 | * @param string $mailbox 1179 | * 1180 | * @return array 1181 | */ 1182 | public function getQuotaRoot($mailbox) 1183 | { 1184 | $quota = imap_get_quotaroot($this->imap, "user.".$mailbox); 1185 | return $quota; 1186 | } 1187 | 1188 | /** 1189 | * Get uid from id 1190 | * 1191 | * @param integer $id 1192 | * @return integer 1193 | */ 1194 | public function getUid($id) 1195 | { 1196 | return imap_uid($this->imap, $id); 1197 | } 1198 | 1199 | /** 1200 | * Get id from uid 1201 | * 1202 | * @param int $uid 1203 | * @return integer 1204 | */ 1205 | public function getId($uid) 1206 | { 1207 | return imap_msgno($this->imap, $uid); 1208 | } 1209 | 1210 | /** 1211 | * Send an email 1212 | * 1213 | * @return void 1214 | */ 1215 | public function sendMail() 1216 | { 1217 | $outMessage = new AdapterForOutgoingMessage(self::$connectConfig); 1218 | $outMessage->send(); 1219 | } 1220 | 1221 | /** 1222 | * Check message id 1223 | * 1224 | * @param integer $id 1225 | * @return void 1226 | * @throws ImapClientException 1227 | */ 1228 | private function checkMessageId($id) 1229 | { 1230 | if(!is_int($id)){ 1231 | throw new ImapClientException('$id must be an integer!'); 1232 | }; 1233 | if($id <= 0){ 1234 | throw new ImapClientException('$id must be greater then 0!'); 1235 | }; 1236 | if($id > $this->countMessages()){ 1237 | throw new ImapClientException('$id does not exist'); 1238 | } 1239 | } 1240 | } 1241 | -------------------------------------------------------------------------------- /ImapClient/ImapClientException.php: -------------------------------------------------------------------------------- 1 | , sergey144010 16 | */ 17 | class ImapClientException extends Exception 18 | { 19 | /** 20 | * Get info about the error(s). 21 | * 22 | * @return string 23 | */ 24 | public function getInfo() 25 | { 26 | $error = $this->getMessage().PHP_EOL; 27 | $error .= 'File: '.$this->getFile().PHP_EOL; 28 | $error .= 'Line: '.$this->getLine().PHP_EOL; 29 | 30 | return $error; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ImapClient/ImapConnect.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class ImapConnect 16 | { 17 | const SERVICE_IMAP = 'imap'; 18 | const SERVICE_POP3 = 'pop3'; 19 | const SERVICE_NNTP = 'nntp'; 20 | const ENCRYPT_SSL = 'ssl'; 21 | const ENCRYPT_TLS = 'tls'; 22 | const ENCRYPT_NOTLS = 'notls'; 23 | const VALIDATE_CERT = 'validate'; 24 | const NOVALIDATE_CERT = 'novalidate'; 25 | const DEBUG = 'debug'; 26 | const SECURE = 'secure'; 27 | const NORSH = 'norsh'; 28 | const READONLY = 'readonly'; 29 | const ANONYMOUS = 'anonymous'; 30 | 31 | 32 | /** 33 | * Current imap stream 34 | * 35 | * @var resource 36 | */ 37 | public $imap; 38 | 39 | /** 40 | * Current mailbox 41 | * 42 | * @var string 43 | */ 44 | public $mailbox; 45 | 46 | /** 47 | * Current flags connecting 48 | * 49 | * @var string 50 | */ 51 | public $flags; 52 | 53 | /** 54 | * The connection to the server 55 | * 56 | * Description of the method imap_open() to look at the link below 57 | * http://php.net/manual/ru/function.imap-open.php 58 | * 59 | * Use as follows: 60 | * 61 | * connect( null, 'username', 'password' ) 62 | * or 63 | * connect([ 64 | * 'username' => 'Name', 65 | * 'password' => 'Pass' 66 | * ]) 67 | * 68 | * @param string|array $mailbox 69 | * @param string $username 70 | * @param string $password 71 | * @param int $options 72 | * @param int $n_retries 73 | * @param array $params 74 | * @return void 75 | * @throws ImapClientException 76 | */ 77 | public function connect($mailbox, $username = null, $password = null, $options = 0, $n_retries = 0, $params = []) 78 | { 79 | /* 80 | * If first parameter method is array 81 | */ 82 | if(isset($mailbox) && is_array($mailbox)) { 83 | $config = $mailbox; 84 | if(isset($config['mailbox'])){ 85 | $mailbox = $config['mailbox']; 86 | }else{ 87 | $mailbox = null; 88 | }; 89 | if(isset($config['username'])){ 90 | $username = $config['username']; 91 | }else{ 92 | $username = null; 93 | }; 94 | if(isset($config['password'])){ 95 | $password = $config['password']; 96 | }else{ 97 | $password = null; 98 | }; 99 | if(isset($config['options'])){ 100 | $options = $config['options']; 101 | }else{ 102 | $options = 0; 103 | }; 104 | if(isset($config['n_retries'])){ 105 | $n_retries = $config['n_retries']; 106 | }else{ 107 | $n_retries = 0; 108 | }; 109 | if(isset($config['params'])){ 110 | $params = $config['params']; 111 | }else{ 112 | $params = []; 113 | }; 114 | }; 115 | 116 | if (!function_exists('imap_open')) { 117 | throw new ImapClientException('Imap function not available'); 118 | }; 119 | if(!isset($mailbox) && isset($this->mailbox)){ 120 | $mailbox = $this->mailbox; 121 | }; 122 | if(empty($mailbox) || is_bool($mailbox)){ 123 | throw new ImapClientException('Mailbox is not installed'); 124 | }; 125 | if(!is_string($mailbox)){ 126 | throw new ImapClientException('Mailbox must be an string'); 127 | }; 128 | if(!is_string($username)){ 129 | throw new ImapClientException('Username must be an string'); 130 | }; 131 | if(!is_string($password)){ 132 | throw new ImapClientException('Password must be an string'); 133 | }; 134 | if(!is_int($options)){ 135 | throw new ImapClientException('Options must be an integer'); 136 | }; 137 | if(!is_int($n_retries)){ 138 | throw new ImapClientException('N_retries must be an integer'); 139 | }; 140 | if(isset($params) && !is_array($params)){ 141 | throw new ImapClientException('Params must be an array'); 142 | }; 143 | 144 | if(empty($options) && empty($n_retries) && empty($params)){ 145 | $this->imap = @imap_open($mailbox, $username , $password); 146 | }else{ 147 | $this->imap = @imap_open($mailbox, $username , $password, $options, $n_retries, $params); 148 | }; 149 | if ($this->imap === false) { 150 | throw new ImapClientException('Error connecting to '.$mailbox); 151 | }; 152 | } 153 | 154 | /** 155 | * Set string mailbox 156 | * 157 | * @param string $mailbox 158 | * @return void 159 | */ 160 | public function setMailbox($mailbox) 161 | { 162 | $this->mailbox = $mailbox; 163 | } 164 | 165 | /** 166 | * Get string mailbox 167 | * 168 | * @return object 169 | */ 170 | public function getMailbox() 171 | { 172 | return $this->mailbox; 173 | } 174 | 175 | /** 176 | * Get string response mailbox 177 | * 178 | * @return string 179 | */ 180 | public function getResponseMailbox() 181 | { 182 | $imap_obj = imap_check($this->imap); 183 | return $imap_obj->Mailbox; 184 | } 185 | 186 | /** 187 | * Get the imap resource 188 | * 189 | * @return resource 190 | */ 191 | public function getImap() 192 | { 193 | return $this->imap; 194 | } 195 | 196 | /** 197 | * Get the flag string 198 | * 199 | * @return string 200 | */ 201 | public function getFlags() 202 | { 203 | return $this->flags; 204 | } 205 | 206 | /** 207 | * Prepare Mailbox string 208 | * 209 | * Sets $this->mailbox to this type of 210 | * {server.imap:431/imap/ssl/novalidate-cert}INBOX 211 | * 212 | * Use as follows: 213 | * 214 | * prepareMailbox( null, $port = 431, null) 215 | * or 216 | * prepareMailbox([ 217 | * 'port' => 431 218 | * ]) 219 | * 220 | * @param string|array $remote_system_name 221 | * @param string $port 222 | * @param string $flags can use prepareFlags() method but not necessarily 223 | * @param string $mailbox_name 224 | * @return void 225 | * @throws ImapClientException 226 | */ 227 | public function prepareMailbox($remote_system_name = null, $port = null, $flags = null, $mailbox_name = null) 228 | { 229 | /* 230 | * If first parameter method is array 231 | */ 232 | if(isset($remote_system_name) && is_array($remote_system_name)){ 233 | $config = $remote_system_name; 234 | if(isset($config['remote_system_name'])){ 235 | $remote_system_name = $config['remote_system_name']; 236 | }else{ 237 | $remote_system_name = null; 238 | }; 239 | if(isset($config['port'])){ 240 | $port = $config['port']; 241 | }else{ 242 | $port = null; 243 | }; 244 | if(isset($config['flags'])){ 245 | $flags = $config['flags']; 246 | }else{ 247 | $flags = null; 248 | }; 249 | if(isset($config['mailbox_name'])){ 250 | $mailbox_name = $config['mailbox_name']; 251 | }else{ 252 | $mailbox_name = null; 253 | }; 254 | }; 255 | 256 | if(!isset($remote_system_name) && isset($this->mailbox)){ 257 | $remote_system_name = $this->mailbox; 258 | }; 259 | if(empty($remote_system_name)){ 260 | throw new ImapClientException('Mailbox is not installed'); 261 | }; 262 | /* 263 | if(is_null($port) && is_null($flags) && is_null($mailbox_name)){ 264 | $this->mailbox = $remote_system_name; 265 | return; 266 | }; 267 | */ 268 | if(isset($port)){ 269 | $port = ':'.$port; 270 | }; 271 | if(!isset($flags) && isset($this->flags)){ 272 | $flags = $this->flags; 273 | }; 274 | $this->mailbox = '{'.$remote_system_name.$port.$flags.'}'.$mailbox_name; 275 | } 276 | 277 | /** 278 | * Prepare Flags 279 | * 280 | * http://php.net/manual/ru/function.imap-open.php 281 | * Section - mailbox - Optional flags for names 282 | * 283 | * Use as follows: 284 | * 285 | * prepareFlags( null, null, null, ImapConnect::VALIDATE_CERT, null) 286 | * or 287 | * prepareFlags([ 288 | * 'validateCertificates' => ImapConnect::VALIDATE_CERT 289 | * ]) 290 | * 291 | * @param string|array $service use appropriate constant like ImapConnect::SERVICE_IMAP 292 | * @param string $encrypt use appropriate constant 293 | * @param string $validateCertificates use appropriate constant 294 | * @param string $secure use appropriate constant 295 | * @param string $norsh use appropriate constant 296 | * @param string $readonly use appropriate constant 297 | * @param string $anonymous use appropriate constant 298 | * @param string $debug use appropriate constant 299 | * @return string|null 300 | */ 301 | public function prepareFlags( 302 | $service = null, 303 | $encrypt = null, 304 | $validateCertificates = null, 305 | $secure = null, 306 | $norsh = null, 307 | $readonly = null, 308 | $anonymous = null, 309 | $debug = null 310 | ) 311 | { 312 | /* 313 | * If first parameter method is array 314 | */ 315 | if(isset($service) && is_array($service)){ 316 | 317 | $config = $service; 318 | 319 | if(isset($config['service'])){ 320 | $service = $config['service']; 321 | }else{ 322 | $service = null; 323 | }; 324 | if(isset($config['encrypt'])){ 325 | $encrypt = $config['encrypt']; 326 | }else{ 327 | $encrypt = null; 328 | }; 329 | if(isset($config['validateCertificates'])){ 330 | $validateCertificates = $config['validateCertificates']; 331 | }else{ 332 | $validateCertificates = null; 333 | }; 334 | if(isset($config['secure'])){ 335 | $secure = $config['secure']; 336 | }else{ 337 | $secure = null; 338 | }; 339 | if(isset($config['norsh'])){ 340 | $norsh = $config['norsh']; 341 | }else{ 342 | $norsh = null; 343 | }; 344 | if(isset($config['readonly'])){ 345 | $readonly = $config['readonly']; 346 | }else{ 347 | $readonly = null; 348 | }; 349 | if(isset($config['anonymous'])){ 350 | $anonymous = $config['anonymous']; 351 | }else{ 352 | $anonymous = null; 353 | }; 354 | if(isset($config['debug'])){ 355 | $debug = $config['debug']; 356 | }else{ 357 | $debug = null; 358 | }; 359 | }; 360 | 361 | $flags = null; 362 | if(isset($service) && $service === self::SERVICE_IMAP){ 363 | $flags .= '/imap'; 364 | }; 365 | if(isset($service) && $service === self::SERVICE_POP3){ 366 | $flags .= '/pop3'; 367 | }; 368 | if(isset($service) && $service === self::SERVICE_NNTP){ 369 | $flags .= '/nntp'; 370 | }; 371 | if(isset($encrypt) && $encrypt === self::ENCRYPT_NOTLS){ 372 | $flags .= '/notls'; 373 | }; 374 | if(isset($encrypt) && $encrypt === self::ENCRYPT_SSL){ 375 | $flags .= '/ssl'; 376 | }; 377 | if(isset($encrypt) && $encrypt === self::ENCRYPT_TLS){ 378 | $flags .= '/tls'; 379 | }; 380 | if(isset($validateCertificates) && $validateCertificates === self::VALIDATE_CERT){ 381 | $flags .= '/validate-cert'; 382 | }; 383 | if(isset($validateCertificates) && $validateCertificates === self::NOVALIDATE_CERT){ 384 | $flags .= '/novalidate-cert'; 385 | }; 386 | if(isset($secure) && $secure === self::SECURE){ 387 | $flags .= '/secure'; 388 | }; 389 | if(isset($norsh) && $norsh === self::NORSH){ 390 | $flags .= '/norsh'; 391 | }; 392 | if(isset($readonly) && $readonly === self::READONLY){ 393 | $flags .= '/readonly'; 394 | }; 395 | if(isset($anonymous) && $anonymous === self::ANONYMOUS){ 396 | $flags .= '/anonymous'; 397 | }; 398 | if(isset($debug) && $debug === self::DEBUG){ 399 | $flags .= '/debug'; 400 | }; 401 | $this->flags = $flags; 402 | } 403 | } 404 | -------------------------------------------------------------------------------- /ImapClient/IncomingMessage.php: -------------------------------------------------------------------------------- 1 | header; 16 | * $incomingMessage->message; 17 | * $incomingMessage->attachments; 18 | * $incomingMessage->section; 19 | * $incomingMessage->structure; 20 | * $incomingMessage->debug; 21 | * ``` 22 | * And marks the message read. 23 | * TODO: Format class correctly. 24 | * 25 | * @copyright Copyright (c) Tobias Zeising (http://www.aditu.de) 26 | * @author Sergey144010 27 | */ 28 | class IncomingMessage 29 | { 30 | /** 31 | * Used to handle sections of the e-mail easier. 32 | */ 33 | const SECTION_ATTACHMENTS = 1; 34 | 35 | /** 36 | * Used to handle sections of the e-mail easier. 37 | */ 38 | const SECTION_BODY = 2; 39 | 40 | /** 41 | * Do not use decode incoming message. 42 | */ 43 | const NOT_DECODE = 'not_decode'; 44 | 45 | /** 46 | * Use decode incoming message. 47 | */ 48 | const DECODE = 'decode'; 49 | 50 | /** 51 | * Header of the message. 52 | * 53 | * @var object 54 | */ 55 | public $header; 56 | 57 | /** 58 | * The message. 59 | * 60 | * @var object 61 | */ 62 | public $message; 63 | 64 | /** 65 | * Attachments. 66 | * 67 | * @var array 68 | */ 69 | public $attachments; 70 | 71 | /** 72 | * Section of the message. 73 | * 74 | * @var string|array 75 | */ 76 | public $section; 77 | 78 | /** 79 | * Structure of the message. 80 | * 81 | * @var object 82 | */ 83 | public $structure; 84 | 85 | /** 86 | * Debug on or off. 87 | * 88 | * @var object 89 | */ 90 | public $debug; 91 | 92 | /** 93 | * The imap string. 94 | * 95 | * @var resource 96 | */ 97 | private $imapStream; 98 | 99 | /** 100 | * ID of the message. 101 | * 102 | * @var int 103 | */ 104 | private $id; 105 | 106 | /** 107 | * UID of the message. 108 | * 109 | * @var int 110 | */ 111 | private $uid; 112 | 113 | /** 114 | * Count the attachments. 115 | * 116 | * @var int 117 | */ 118 | private $countAttachment; 119 | 120 | /** 121 | * Disable/enable decode current incoming message. 122 | * 123 | * @var string 124 | */ 125 | private $_decode; 126 | 127 | /** 128 | * Called when the class has a new instance made of it. 129 | * 130 | * @param resource $imapStream 131 | * @param int $id 132 | * @param string $decode 133 | * 134 | * @return IncomingMessage 135 | */ 136 | public function __construct($imapStream, $id, $decode = self::DECODE) 137 | { 138 | $this->imapStream = $imapStream; 139 | if (is_array($id)) { 140 | $identifier = $id; 141 | if (isset($identifier['id'])) { 142 | $this->id = $identifier['id']; 143 | $this->uid = null; 144 | } 145 | if (isset($identifier['uid'])) { 146 | $this->uid = $identifier['uid']; 147 | $this->id = null; 148 | } 149 | unset($identifier); 150 | } 151 | if (is_int($id)) { 152 | $this->id = $id; 153 | } 154 | 155 | if (isset($decode)) { 156 | $this->_decode = $decode; 157 | } 158 | 159 | $this->init(); 160 | } 161 | 162 | /** 163 | * Main process. 164 | * 165 | * @return void 166 | */ 167 | protected function init() 168 | { 169 | $structure = $this->imapFetchstructure(); 170 | $this->structure = $structure; 171 | if (isset($structure->parts)) { 172 | $countSection = count($structure->parts); 173 | $this->countAttachment = $countSection - 1; 174 | } 175 | $this->getCountSection(); 176 | $this->getHeader(); 177 | $this->getAttachments(); 178 | $this->getBody(); 179 | if ($this->_decode === self::DECODE) { 180 | $this->decode(); 181 | } 182 | } 183 | 184 | /** 185 | * Get headers in the current message. 186 | * 187 | * Set 188 | * ```php 189 | * $this->header 190 | * $this->header->details 191 | * ``` 192 | * 193 | * @return void 194 | */ 195 | protected function getHeader() 196 | { 197 | $header = $this->imapFetchOverview(); 198 | $this->header = $header[0]; 199 | $this->header->details = $this->imapHeaderInfo(); 200 | } 201 | 202 | /** 203 | * Returns current object. 204 | * 205 | * Set $this->debug 206 | * 207 | * @return void 208 | */ 209 | public function debug() 210 | { 211 | $this->debug = $this; 212 | } 213 | 214 | /** 215 | * Get count section. 216 | * 217 | * We take $this->section and make a simple array from an array of arrays. 218 | * If getRecursiveSections($this->structure) set $this->section to NULL, 219 | * then we think that there is only one section in the letter. 220 | * We install $this->section[0] = [0], 221 | * and then we will take this into account in subsequent processing. 222 | * Namely here getSection() and $this->getSectionStructure() 223 | * or getSectionStructureFromIncomingStructure(). 224 | * Because if the message id is correct and the structure is returned, 225 | * then there is exactly one section in the message. 226 | * 227 | * @return array sections 228 | */ 229 | protected function getCountSection() 230 | { 231 | $this->getRecursiveSections($this->structure); 232 | $sections = array(); 233 | if (!isset($this->section)) { 234 | $this->section[0] = array(0); 235 | } 236 | foreach ($this->section as $array) { 237 | foreach ($array as $section) { 238 | $sections[] = $section; 239 | } 240 | } 241 | $sections = array_unique($sections); 242 | sort($sections); 243 | $this->section = $sections; 244 | 245 | return $this->section; 246 | } 247 | 248 | /** 249 | * Bypasses the recursive parts current message. 250 | * 251 | * Counts sections based on $obj->parts. 252 | * And sets $this->section as an array of arrays or null. 253 | * Null if $obj->parts is not. 254 | * 255 | * @param object $obj 256 | * @param string $before 257 | * 258 | * @return void 259 | */ 260 | protected function getRecursiveSections($obj, $before = null) 261 | { 262 | if (!isset($obj->parts)) { 263 | return; 264 | } 265 | $countParts = count($obj->parts); 266 | $out = array(); 267 | $beforeSave = $before; 268 | foreach ($obj->parts as $key => $subObj) { 269 | if (!isset($beforeSave)) { 270 | $before = ($key + 1); 271 | } else { 272 | $before = $beforeSave.'.'.($key + 1); 273 | } 274 | $this->getRecursiveSections($subObj, $before); 275 | $out[] = (string) $before; 276 | } 277 | $this->section[] = $out; 278 | } 279 | 280 | /** 281 | * Gets all sections, or if parameter is specified sections by type. 282 | * 283 | * @param string $type 284 | * 285 | * @throws ImapClientException 286 | * 287 | * @return array 288 | */ 289 | protected function getSections($type = null) 290 | { 291 | if (!$type) { 292 | return $this->section; 293 | } 294 | $types = null; 295 | switch ($type) { 296 | case self::SECTION_ATTACHMENTS: 297 | $types = TypeAttachments::get(); 298 | break; 299 | case self::SECTION_BODY: 300 | $types = TypeBody::get(); 301 | break; 302 | default: 303 | throw new ImapClientException('Section type not recognised/supported'); 304 | break; 305 | } 306 | $sections = array(); 307 | foreach ($this->section as $section) { 308 | $obj = $this->getSectionStructure($section); 309 | if (!isset($obj->subtype)) { 310 | continue; 311 | } 312 | if (in_array($obj->subtype, $types, false)) { 313 | $sections[] = $section; 314 | } 315 | } 316 | 317 | return $sections; 318 | } 319 | 320 | /** 321 | * Get attachments in the current message. 322 | * 323 | * Set 324 | * $this->attachments->name 325 | * $this->attachments->body 326 | * $this->attachments->info 327 | * 328 | * @return array 329 | */ 330 | protected function getAttachments() 331 | { 332 | $attachments = array(); 333 | foreach ($this->getSections(self::SECTION_ATTACHMENTS) as $section) { 334 | $obj = $this->getSection($section); 335 | $attachment = new IncomingMessageAttachment($obj); 336 | $objNew = new \stdClass(); 337 | $objNew->name = $attachment->name; 338 | $objNew->body = $attachment->body; 339 | $objNew->info = $obj; 340 | $attachments[] = $objNew; 341 | } 342 | $this->attachments = $attachments; 343 | } 344 | 345 | /** 346 | * Get body current message. 347 | * 348 | * Set 349 | * $this->message->$subtype 350 | * $this->message->$subtype->charset 351 | * $this->message->text 352 | * $this->message->info[] 353 | * $this->message->types[] 354 | * 355 | * @return object 356 | */ 357 | protected function getBody() 358 | { 359 | $objNew = new \stdClass(); 360 | $i = 1; 361 | $subType = new SubtypeBody(); 362 | foreach ($this->getSections(self::SECTION_BODY) as $section) { 363 | $obj = $this->getSection($section, array('class' => $subType)); 364 | if(!is_object($obj) || !$obj->__get('structure')) { 365 | continue; 366 | } 367 | $subtype = strtolower($obj->__get('structure')->subtype); 368 | if(!empty($subtype)) { 369 | if (!isset($objNew->$subtype)) { 370 | $objNew->$subtype = $obj; 371 | } else { 372 | $subtype = $subtype.'_'.$i; 373 | $objNew->$subtype = $obj; 374 | $i++; 375 | } 376 | $objNew->info[] = $obj; 377 | $objNew->types[] = $subtype; 378 | /* 379 | * Set charset 380 | */ 381 | foreach ($objNew->$subtype->__get('structure')->parameters as $parameter) { 382 | $attribute = strtolower($parameter->attribute); 383 | if ($attribute === 'charset') { 384 | $value = strtolower($parameter->value); 385 | /* 386 | * Here must be array, but 387 | */ 388 | //$objNew->$subtype->charset[] = $value; 389 | $objNew->$subtype->charset = $value; 390 | } 391 | } 392 | } 393 | } 394 | if (isset($objNew->plain)) { 395 | switch ($objNew->plain->structure->encoding) { 396 | case 3: 397 | $objNew->text = imap_base64(mb_convert_encoding( $objNew->plain, "utf-8", $objNew->plain->charset )); 398 | break; 399 | default: 400 | $objNew->text = quoted_printable_decode(mb_convert_encoding( $objNew->plain, "utf-8", $objNew->plain->charset )); 401 | break; 402 | } 403 | $objNew->types[] = 'text'; 404 | } else { 405 | $objNew->text = null; 406 | } 407 | $this->message = $objNew; 408 | } 409 | 410 | /** 411 | * Get a section message. 412 | * 413 | * Return object with 2 properties: 414 | * $obj->structure 415 | * $obj->body 416 | * 417 | * @param string $section 418 | * @param array|null $options have one option $options['class']. It create object, which must be instance \SSilence\ImapClient\Section. 419 | * 420 | * @throws ImapClientException 421 | * 422 | * @return \SSilence\ImapClient\Section object 423 | */ 424 | public function getSection($section, $options = null) 425 | { 426 | if (isset($options['class'])) { 427 | $sectionObj = new $options['class'](); 428 | if ($sectionObj instanceof Section) { 429 | } else { 430 | throw new ImapClientException('Incoming class not instance \SSilence\ImapClient\Section'); 431 | } 432 | } else { 433 | $sectionObj = new Section(); 434 | } 435 | if ($section === 0) { 436 | /* 437 | If the message id is correct and the structure is returned, 438 | then there is exactly one section in the message. 439 | */ 440 | $sectionObj->structure = $this->imapBodystruct(1); 441 | $sectionObj->body = $this->imapFetchbody(1); 442 | } else { 443 | $sectionObj->structure = $this->imapBodystruct($section); 444 | $sectionObj->body = $this->imapFetchbody($section); 445 | } 446 | 447 | return $sectionObj; 448 | } 449 | 450 | /** 451 | * Alias for getSectionStructureFromIncomingStructure();. 452 | * 453 | * @param string $section 454 | * 455 | * @return object|null 456 | */ 457 | public function getSectionStructure($section) 458 | { 459 | return $this->getSectionStructureFromIncomingStructure($section); 460 | } 461 | 462 | /** 463 | * Get section structure from incoming structure. 464 | * 465 | * @param string $section 466 | * 467 | * @return object|null 468 | */ 469 | protected function getSectionStructureFromIncomingStructure($section) 470 | { 471 | $pos = strpos($section, '.'); 472 | if ($pos === false) { 473 | $section = (int) $section; 474 | if ($section === 0) { 475 | return $this->structure; 476 | } 477 | 478 | return $this->structure->parts[($section - 1)]; 479 | } 480 | $sections = explode('.', $section); 481 | $count = count($sections); 482 | $outObject = null; 483 | foreach ($sections as $section) { 484 | $section = (int) $section; 485 | if (!isset($outObject)) { 486 | $outObject = $this->getObjectStructureFromParts($this->structure, ($section - 1)); 487 | } else { 488 | $outObject = $this->getObjectStructureFromParts($outObject, ($section - 1)); 489 | } 490 | } 491 | 492 | return $outObject; 493 | } 494 | 495 | /** 496 | * Get object structure from parts. 497 | * 498 | * @param object $inObject 499 | * @param int $part 500 | * 501 | * @return object 502 | */ 503 | protected function getObjectStructureFromParts($inObject, $part) 504 | { 505 | return $inObject->parts[$part]; 506 | } 507 | 508 | /** 509 | * Get a specific section. 510 | * 511 | * @param string $section 512 | * 513 | * @return string 514 | */ 515 | protected function imapFetchbody($section) 516 | { 517 | /* 518 | * Update note: We must add FT_PEEK to perserve the unread status of the email. 519 | * Documentation of this can see seen here: http://php.net/manual/en/function.imap-fetchbody.php under options 520 | */ 521 | return imap_fetchbody($this->imapStream, $this->id, $section, FT_PEEK); 522 | } 523 | 524 | /** 525 | * Structure all messages. 526 | * 527 | * @return object 528 | */ 529 | protected function imapFetchstructure() 530 | { 531 | return imap_fetchstructure($this->imapStream, $this->id); 532 | } 533 | 534 | /** 535 | * Structure specific section. 536 | * 537 | * @param string $section 538 | * 539 | * @return object 540 | */ 541 | protected function imapBodystruct($section) 542 | { 543 | return imap_bodystruct($this->imapStream, $this->id, $section); 544 | } 545 | 546 | /** 547 | * Fetch a quick "Overview" on a message. 548 | * 549 | * @see http://php.net/manual/ru/function.imap-fetch-overview.php 550 | * 551 | * @throws ImapClientException 552 | * 553 | * @return object 554 | */ 555 | protected function imapFetchOverview() 556 | { 557 | if (isset($this->id) && isset($this->uid)) { 558 | throw new ImapClientException('What to use id or uid?'); 559 | } 560 | $sequence = null; 561 | $options = null; 562 | if (isset($this->id) && !isset($this->uid)) { 563 | $sequence = $this->id; 564 | $options = null; 565 | } 566 | if (!isset($this->id) && isset($this->uid)) { 567 | $sequence = $this->uid; 568 | $options = FT_UID; 569 | } 570 | 571 | return imap_fetch_overview($this->imapStream, $sequence, $options); 572 | } 573 | 574 | /** 575 | * Imap Header Info. 576 | * 577 | * Wrapper for http://php.net/manual/ru/function.imap-headerinfo.php 578 | * 579 | * @see http://php.net/manual/ru/function.imap-headerinfo.php 580 | * @see http://php.net/manual/en/function.imap-headerinfo.php#98809 581 | * 582 | * @return object 583 | */ 584 | protected function imapHeaderInfo() 585 | { 586 | return imap_rfc822_parse_headers(imap_fetchheader($this->imapStream, $this->id)); 587 | } 588 | 589 | /** 590 | * Convert to utf8 if necessary. 591 | * 592 | * @param string $str utf8 encoded string 593 | * 594 | * @return string 595 | */ 596 | public function convertToUtf8($str, $charset = 'ISO-8859-1') 597 | { 598 | if ($charset == 'default') { 599 | $charset = 'utf-8'; 600 | } 601 | 602 | $encoding = mb_detect_encoding($str, mb_detect_order(), false); 603 | 604 | if($encoding == 'UTF-8'){ 605 | $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8'); 606 | } 607 | 608 | $str = iconv(mb_detect_encoding($str, mb_detect_order(), false), "UTF-8//IGNORE", $str); 609 | // $str = iconv($charset, 'UTF-8//IGNORE', $str); 610 | 611 | return $str; 612 | } 613 | 614 | /** 615 | * Wrapper for imap_mime_header_decode() 616 | * http://php.net/manual/ru/function.imap-mime-header-decode.php. 617 | * 618 | * @see http://php.net/manual/ru/function.imap-mime-header-decode.php 619 | * 620 | * @param string $string 621 | * 622 | * @return array 623 | */ 624 | protected function imapMimeHeaderDecode($string) 625 | { 626 | return imap_mime_header_decode($string); 627 | } 628 | 629 | /** 630 | * Decodes and glues the title bar 631 | * http://php.net/manual/ru/function.imap-mime-header-decode.php. 632 | * 633 | * @see http://php.net/manual/ru/function.imap-mime-header-decode.php 634 | * 635 | * @param string $string 636 | * 637 | * @return string 638 | */ 639 | protected function mimeHeaderDecode($string) 640 | { 641 | $cache = null; 642 | $array = $this->imapMimeHeaderDecode($string); 643 | foreach ($array as $object) { 644 | $cache .= $this->convertToUtf8($object->text, $object->charset); 645 | } 646 | 647 | return $cache; 648 | } 649 | 650 | /** 651 | * Decode incoming message. 652 | * 653 | * @return void 654 | */ 655 | protected function decode() 656 | { 657 | $this->decodeHeader(); 658 | $this->decodeBody(); 659 | $this->decodeAttachments(); 660 | } 661 | 662 | /** 663 | * Decode header. 664 | * 665 | * @return void 666 | */ 667 | protected function decodeHeader() 668 | { 669 | if (isset($this->header->subject)) { 670 | $this->header->subject = $this->mimeHeaderDecode($this->header->subject); 671 | } 672 | if (isset($this->header->details->subject)) { 673 | $this->header->details->subject = $this->mimeHeaderDecode($this->header->details->subject); 674 | } 675 | if (isset($this->header->details->Subject)) { 676 | $this->header->details->Subject = $this->mimeHeaderDecode($this->header->details->Subject); 677 | } 678 | if (isset($this->header->from)) { 679 | $this->header->from = $this->mimeHeaderDecode($this->header->from); 680 | } 681 | if (isset($this->header->to)) { 682 | $this->header->to = $this->mimeHeaderDecode($this->header->to); 683 | } 684 | } 685 | 686 | /** 687 | * Decode attachments. 688 | * 689 | * @return void 690 | */ 691 | protected function decodeAttachments() 692 | { 693 | foreach ($this->attachments as $key => $attachment) { 694 | if( 695 | is_object($attachment) && 696 | property_exists($attachment, 'info') && 697 | is_object($attachment->info) && 698 | property_exists($attachment->info, 'structure') && 699 | is_object($attachment->info->structure) && 700 | property_exists($attachment->info->structure, 'encoding') 701 | ) { 702 | /* 703 | * Decode body 704 | */ 705 | switch ($attachment->info->structure->encoding) { 706 | case 3: 707 | $this->attachments[$key]->body = imap_base64($attachment->body); 708 | break; 709 | case 4: 710 | $this->attachments[$key]->body = quoted_printable_decode($attachment->body); 711 | break; 712 | } 713 | /* 714 | * Decode name 715 | */ 716 | $this->attachments[$key]->name = $this->mimeHeaderDecode($attachment->name); 717 | } 718 | } 719 | } 720 | 721 | /** 722 | * Decode body. 723 | * 724 | * @return void 725 | */ 726 | protected function decodeBody() 727 | { 728 | if(is_object($this->message)){ 729 | if(property_exists($this->message, 'types')) { 730 | foreach ($this->message->types as $typeMessage) { 731 | if(is_object($this->message->$typeMessage)){ 732 | switch ($this->message->$typeMessage->structure->encoding) { 733 | case 3: 734 | $this->message->$typeMessage->body = imap_base64($this->message->$typeMessage->body); 735 | break; 736 | case 4: 737 | $this->message->$typeMessage->body = imap_qprint($this->message->$typeMessage->body); 738 | break; 739 | } 740 | } 741 | 742 | if(is_object($this->message->$typeMessage)){ 743 | if ($this->message->$typeMessage->charset) { 744 | $this->message->$typeMessage->body = $this->convertToUtf8($this->message->$typeMessage->body, $this->message->$typeMessage->charset); 745 | } 746 | } 747 | } 748 | } 749 | } 750 | } 751 | 752 | /** 753 | * Info about this object. 754 | * 755 | * @return array 756 | */ 757 | public function __debugInfo() 758 | { 759 | return array( 760 | 'header' => $this->header, 761 | 'message' => $this->message, 762 | 'attachments' => $this->attachments, 763 | 'section' => $this->section, 764 | 'structure' => $this->structure, 765 | 'debug' => $this->debug, 766 | ); 767 | } 768 | 769 | /** 770 | * Get the id property of the message 771 | * 772 | * @return int 773 | */ 774 | public function getID() 775 | { 776 | return $this->id; 777 | } 778 | } 779 | -------------------------------------------------------------------------------- /ImapClient/IncomingMessageAttachment.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class IncomingMessageAttachment 16 | { 17 | /** 18 | * Name current attachment. 19 | * 20 | * @var string 21 | */ 22 | public $name; 23 | 24 | /** 25 | * Body current attachment. 26 | * 27 | * @var string 28 | */ 29 | public $body; 30 | 31 | /** 32 | * Incoming object. 33 | * 34 | * Incoming SSilence\ImapClient\Section object 35 | * 36 | * @var Section 37 | */ 38 | private $_incomingObject; 39 | 40 | /** 41 | * The constructor. 42 | * 43 | * Set $this->name and $this->body 44 | * 45 | * @param Section $incomingObject 46 | * 47 | * @return IncomingMessageAttachment 48 | */ 49 | public function __construct(Section $incomingObject) 50 | { 51 | $this->_incomingObject = $incomingObject; 52 | $this->getName(); 53 | $this->getBody(); 54 | } 55 | 56 | /** 57 | * Returns the name of the attachment along with file extension. 58 | * 59 | * @return string 60 | */ 61 | protected function getName() 62 | { 63 | // Check for different types of inline attachments. 64 | if (is_object($this->_incomingObject->structure) && 65 | property_exists($this->_incomingObject->structure, 'ifdparameters') && 66 | $this->_incomingObject->structure->ifdparameters) { 67 | foreach ($this->_incomingObject->structure->dparameters as $param) { 68 | if (strtolower($param->attribute) === 'filename') { 69 | $this->name = $param->value; 70 | break; 71 | } 72 | } 73 | } elseif (is_object($this->_incomingObject->structure) && 74 | property_exists($this->_incomingObject->structure, 'ifparameters') && 75 | $this->_incomingObject->structure->ifparameters) { 76 | foreach ($this->_incomingObject->structure->parameters as $param) { 77 | if (strtolower($param->attribute) === 'name') { 78 | $this->name = $param->value; 79 | break; 80 | } 81 | } 82 | } 83 | } 84 | 85 | /** 86 | * Returns the body of the e-mail. 87 | * 88 | * @return string 89 | */ 90 | protected function getBody() 91 | { 92 | $this->body = $this->_incomingObject->body; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /ImapClient/OutgoingMessage.php: -------------------------------------------------------------------------------- 1 | , sergey144010 17 | */ 18 | class OutgoingMessage 19 | { 20 | /** 21 | * Message To. 22 | * 23 | * @see http://php.net/manual/ru/function.imap-mail.php 24 | * 25 | * @var string 26 | */ 27 | private $to; 28 | 29 | /** 30 | * Message Subject. 31 | * 32 | * @see http://php.net/manual/ru/function.imap-mail.php 33 | * 34 | * @var string 35 | */ 36 | private $subject; 37 | 38 | /** 39 | * Message Message. 40 | * 41 | * @see http://php.net/manual/ru/function.imap-mail.php 42 | * 43 | * @var string 44 | */ 45 | private $message; 46 | 47 | /** 48 | * Message additional_headers. 49 | * 50 | * @see http://php.net/manual/ru/function.imap-mail.php 51 | * 52 | * @var 53 | */ 54 | private $additional_headers; 55 | 56 | /** 57 | * Message CC. 58 | * 59 | * @see http://php.net/manual/ru/function.imap-mail.php 60 | * 61 | * @var string 62 | */ 63 | private $cc; 64 | 65 | /** 66 | * Message BCC. 67 | * 68 | * @see http://php.net/manual/ru/function.imap-mail.php 69 | * 70 | * @var 71 | */ 72 | private $bcc; 73 | 74 | /** 75 | * Message rpath. 76 | * 77 | * @see http://php.net/manual/ru/function.imap-mail.php 78 | * 79 | * @var string 80 | */ 81 | private $rpath; 82 | 83 | /** 84 | * For send() method. 85 | */ 86 | private $properties; 87 | 88 | /** 89 | * For createMimeMessage() method. 90 | */ 91 | private $envelope; 92 | 93 | /** 94 | * For createBody() method. 95 | * 96 | * @var string 97 | */ 98 | private $body; 99 | 100 | /** 101 | * Send message via imap_mail. 102 | * 103 | * @return void 104 | */ 105 | public function send() 106 | { 107 | $mimeMessage = $this->createMimeMessage(); 108 | $this->message = $mimeMessage; 109 | $this->preparingSend(); 110 | imap_mail( 111 | $this->properties->to, 112 | $this->properties->subject, 113 | $this->properties->message, 114 | $this->properties->additional_headers, 115 | $this->properties->cc, 116 | $this->properties->bcc, 117 | $this->properties->rpath 118 | ); 119 | } 120 | 121 | /** 122 | * Preparing properties. 123 | * 124 | * @return void 125 | */ 126 | protected function preparingSend() 127 | { 128 | $allowedProperties = array( 129 | 'to', 'subject', 'message', 'additional_headers', 'cc', 'bcc', 'rpath', 130 | ); 131 | $properties = array( 132 | 'to' => $this->to, 133 | 'subject' => $this->subject, 134 | 'message' => $this->message, 135 | 'additional_headers' => $this->additional_headers, 136 | 'cc' => $this->cc, 137 | 'bcc' => $this->bcc, 138 | 'rpath' => $this->rpath, 139 | ); 140 | $this->properties = Helper::preparingProperties($properties, $allowedProperties); 141 | } 142 | 143 | /** 144 | * Create Mime Message. 145 | * 146 | * @see http://php.net/manual/ru/function.imap-mail-compose.php 147 | * 148 | * @return string 149 | */ 150 | public function createMimeMessage() 151 | { 152 | $this->createBody(); 153 | 154 | $envelopeAllowedType = array( 155 | 'remail', 'return_path', 'date', 'from', 'reply_to', 'in_reply_to', 156 | 'subject', 'to', 'cc', 'bcc', 'message_id', 'custom_headers', 157 | ); 158 | /* @var $envelope array */ 159 | $envelope = Helper::preparingProperties($this->envelope, $envelopeAllowedType, Helper::OUT_ARRAY); 160 | $bodyAllowedType = array( 161 | 'type', 'encoding', 'charset', 'type.parameters', 'subtype', 162 | 'id', 'description', 'disposition.type', 'disposition', 'contents.data', 163 | 'lines', 'bytes', 'md5', 164 | ); 165 | /* @var $body array */ 166 | foreach ($this->body as $key => $part) { 167 | $this->body[$key] = Helper::preparingProperties($part, $bodyAllowedType, Helper::OUT_ARRAY); 168 | } 169 | $body = $this->body; 170 | 171 | return imap_mail_compose($envelope, $body); 172 | } 173 | 174 | /** 175 | * Create body. 176 | * 177 | * @return void 178 | */ 179 | public function createBody() 180 | { 181 | $this->envelope['date'] = '29.03.2017'; 182 | $this->envelope['message_id'] = '81'; 183 | 184 | $part1['type'] = TYPEMULTIPART; 185 | $part1['subtype'] = 'mixed'; 186 | 187 | $part3['type'] = TYPETEXT; 188 | $part3['subtype'] = 'plain'; 189 | $part3['description'] = 'description3'; 190 | $part3['contents.data'] = "contents.data3\n\n\n\t"; 191 | 192 | $body[1] = $part1; 193 | //$body[2] = $part2; 194 | $body[3] = $part3; 195 | 196 | $this->body = $body; 197 | } 198 | 199 | /** 200 | * Set attachment. 201 | */ 202 | public function setAttachment() 203 | { 204 | } 205 | 206 | /** 207 | * Set From. 208 | * 209 | * @param string $from 210 | */ 211 | public function setFrom($from) 212 | { 213 | $this->envelope['from'] = $from; 214 | $this->additional_headers = 'From: '.$from; 215 | } 216 | 217 | /** 218 | * Set To. 219 | * 220 | * @param string $to 221 | */ 222 | public function setTo($to) 223 | { 224 | $this->to = $to; 225 | $this->envelope['to'] = $to; 226 | } 227 | 228 | /** 229 | * Set Subject. 230 | * 231 | * @param string $subject 232 | */ 233 | public function setSubject($subject) 234 | { 235 | $this->subject = $subject; 236 | $this->envelope['subject'] = $subject; 237 | } 238 | 239 | /** 240 | * Set Message. 241 | * 242 | * @param string $message 243 | */ 244 | public function setMessage($message) 245 | { 246 | $this->message = $message; 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /ImapClient/Section.php: -------------------------------------------------------------------------------- 1 | _structure = $value; 44 | break; 45 | case 'body': 46 | $this->_body = $value; 47 | break; 48 | default: 49 | throw new ImapClientException('Section object have only "structure" and "body" properties.'); 50 | } 51 | } 52 | 53 | /** 54 | * Get current property. 55 | * 56 | * @param string $property 57 | * 58 | * @throws ImapClientException 59 | * 60 | * @return object|string 61 | */ 62 | public function __get($property) 63 | { 64 | switch ($property) { 65 | case 'structure': 66 | return $this->_structure; 67 | break; 68 | case 'body': 69 | return $this->_body; 70 | break; 71 | default: 72 | throw new ImapClientException('Section object have only "structure" and "body" properties.'); 73 | } 74 | } 75 | 76 | /** 77 | * Check isset() current object property. 78 | * 79 | * @param string $property 80 | * 81 | * @throws ImapClientException 82 | * 83 | * @return object|string 84 | */ 85 | public function __isset($property) 86 | { 87 | switch ($property) { 88 | case 'structure': 89 | return $this->_structure; 90 | break; 91 | case 'body': 92 | return $this->_body; 93 | break; 94 | default: 95 | throw new ImapClientException('Section object have only "structure" and "body" properties.'); 96 | } 97 | } 98 | 99 | /** 100 | * Unset current object property. 101 | * 102 | * @param string $property 103 | * 104 | * @throws ImapClientException 105 | */ 106 | public function __unset($property) 107 | { 108 | throw new ImapClientException('Section object not supported unset.'); 109 | } 110 | 111 | /** 112 | * Return $this->_body when object convert to string. 113 | * 114 | * @return string 115 | */ 116 | public function __toString() 117 | { 118 | if (!$this->_body) { 119 | return ''; 120 | } 121 | 122 | return $this->_body; 123 | } 124 | 125 | /** 126 | * Returns the private properties of the object when serializing, 127 | * like this json_encode(). 128 | * 129 | * @return array 130 | */ 131 | public function jsonSerialize() 132 | { 133 | $properties = get_object_vars($this); 134 | $outProperties = array(); 135 | foreach ($properties as $propertie => $value) { 136 | if ($propertie[0] === '_') { 137 | $namePropertie = substr($propertie, 1); 138 | $outProperties[$namePropertie] = $this->$propertie; 139 | } else { 140 | $outProperties[$propertie] = $this->$propertie; 141 | } 142 | } 143 | 144 | return $outProperties; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /ImapClient/SubtypeBody.php: -------------------------------------------------------------------------------- 1 | , sergey144010 14 | */ 15 | class SubtypeBody extends Section 16 | { 17 | /** 18 | * Charset current section body. 19 | * 20 | * @var string 21 | */ 22 | public $charset; 23 | 24 | /** 25 | * This is just a blank function to maybe fix travis.. 26 | */ 27 | public function travisIsABaby() 28 | { 29 | // Blank function 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ImapClient/TypeAttachments.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class TypeAttachments 16 | { 17 | /** 18 | * Types of attachments. 19 | * 20 | * @var array 21 | */ 22 | private static $types = array('JPEG', 'JPG', 'PNG', 'GIF', 'PDF', 'X-MPEG', 'MSWORD', 'OCTET-STREAM', 'TXT', 'TEXT', 'MWORD', 'ZIP', 'MPEG', 'DBASE', 'ACROBAT', 'POWERPOINT', 'BMP', 'BITMAP'); 23 | 24 | /** 25 | * Get the allowed types. 26 | * 27 | * @return array 28 | */ 29 | public static function get() 30 | { 31 | return static::$types; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ImapClient/TypeBody.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | class TypeBody 16 | { 17 | /** 18 | * Types of body's. 19 | * 20 | * @var array 21 | */ 22 | private static $types = array('PLAIN', 'HTML'); 23 | 24 | /** 25 | * Get the allowed types. 26 | * 27 | * @return array 28 | */ 29 | public static function get() 30 | { 31 | return static::$types; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016-2017 SSilence 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Welcome to php-imap-client 2 | =================== 3 | [![Travis button](https://travis-ci.org/SSilence/php-imap-client.svg?branch=master)](https://travis-ci.org/SSilence/php-imap-client) 4 | [![StyleCI button](https://styleci.io/repos/15479057/shield?branch=master)](https://styleci.io/repos/15479057) 5 | 6 | Hello! This is php-imap-client. This is a simple and easy to use class for connecting to imap servers and working with the emails inside. Our well-known features are: 7 | - simple interface 8 | - Get emails and folders 9 | - Move, delete, count emails 10 | - Rename, delete and 11 | - Get attachments 12 | - Many more 13 | 14 | Documentation 15 | ------------- 16 | php-imap-client is heavily documented. The docs can be accessed [here](http://ssilence.github.io/php-imap-client/index.html). 17 | #### Getting started 18 | If you would like help getting started, we have a guide [here](http://ssilence.github.io/php-imap-client/gettingstarted.html). 19 | #### Connecting 20 | If you need help setting up connections. Read [our connecting guide](http://ssilence.github.io/php-imap-client/connecting.html). 21 | #### Error database 22 | Have an error? Read up on our error [database](http://ssilence.github.io/php-imap-client/errordb.html) to see what is going wrong. 23 | #### Installing 24 | Need help installing our library? Check out our install [guide](http://ssilence.github.io/php-imap-client/installing.html). 25 | #### Methods 26 | Want a full list of methods? Check it out [right here](http://ssilence.github.io/php-imap-client/methods.html). 27 | #### Usage 28 | Using this library is not hard. Why not read [our usage guide](http://ssilence.github.io/php-imap-client/usage.html). 29 | #### Examples 30 | Need a nice little pointer? Check out our examples [here](http://ssilence.github.io/php-imap-client/examples.html). 31 | #### Incoming messages 32 | Any message in the inbox is called an incoming message 'round these parts. Read up on them [here](http://ssilence.github.io/php-imap-client/incomingmessage.html). 33 | #### Contributing 34 | If you would like to be a lamb and help us out check out [our contributing guide](http://ssilence.github.io/php-imap-client/contributing.html). 35 | 36 | Copyright info 37 | ---------- 38 | Copyright (c) 2016-2017 Tobias Zeising, tobias.zeising@aditu.de 39 | http://www.aditu.de 40 | Licensed under the MIT license 41 | -------------------------------------------------------------------------------- /autoload.php: -------------------------------------------------------------------------------- 1 | =5.6", 91 | "ext-imap": "*" 92 | }, 93 | "autoload": { 94 | "psr-4": { "SSilence\\ImapClient\\" : "ImapClient" } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /docs/connecting.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/contributing.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/errordb.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/examples.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/gettingstarted.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/guide/connecting.md: -------------------------------------------------------------------------------- 1 | # Default connnection 2 | A default connection is the easy and most uncommon way to connect to an imap sever. Use the code 3 | below to use our default connection method. 4 | ```php 5 | 6 | // Encryption 7 | $imap = new ImapClient($mailbox, $username, $password, $encryption); 8 | // No Encryption 9 | $imap = new ImapClient($mailbox, $username, $password); 10 | ``` 11 | This code does not check for errors and assumeing everything is right. If you are using this in production be sure to check for errors 12 | # Advanced Conecting 13 | We also have ways to change how your connection works. Read the code and examples below to learn some ways to modifiy your connection, 14 | ```php 15 | /* Example 1 16 | * Example 1 is the advanced default connection method 17 | */ 18 | $imap = new ImapClient([ 19 | 'flags' => [ 20 | 'service' => ImapConnect::SERVICE_IMAP, 21 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 22 | /* This NOVALIDATE_CERT is used when the server connecting to the imap 23 | * servers is not https but the imap is. This ignores the failure. 24 | */ 25 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, 26 | ], 27 | 'mailbox' => [ 28 | 'remote_system_name' => 'imap.server.ru', 29 | ], 30 | 'connect' => [ 31 | 'username' => 'user', 32 | 'password' => 'pass' 33 | ] 34 | ]); 35 | 36 | /* Example 2 37 | * Get debug messages 38 | */ 39 | $imap = new ImapClient([ 40 | 'flags' => [ 41 | 'service' => ImapConnect::SERVICE_IMAP, 42 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 43 | 'validateCertificates' => ImapConnect::VALIDATE_CERT, 44 | // Turns debug on or off 45 | 'debug' => ImapConnect::DEBUG, 46 | ], 47 | 'mailbox' => [ 48 | 'remote_system_name' => 'imap.server.ru', 49 | 'port' => 123, 50 | ], 51 | 'connect' => [ 52 | 'username' => 'user', 53 | 'password' => 'pass' 54 | ] 55 | ]); 56 | 57 | /* Example 3 58 | * You can also set the config then connects 59 | */ 60 | ImapClient::setConnectAdvanced(); 61 | ImapClient::setConnectConfig([ 62 | 'flags' => [ 63 | 'service' => ImapConnect::SERVICE_POP3, 64 | 'validateCertificates' => ImapConnect::VALIDATE_CERT, 65 | 'debug' => ImapConnect::DEBUG, 66 | ], 67 | ]); 68 | $imap = new ImapClient(); 69 | 70 | /* Example 5 71 | * Here you can see all the options 72 | */ 73 | $imap = new ImapClient([ 74 | 'flags' => [ 75 | # Service can be ImapConnect::SERVICE_IMAP ,ImapConnect::SERVICE_POP3, ImapConnect::SERVICE_NNTP 76 | 'service' => ImapConnect::SERVICE_IMAP, 77 | # Encrypt can be ImapConnect::ENCRYPT_SSL, ImapConnect::ENCRYPT_TLS, ImapConnect::ENCRYPT_NOTLS 78 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 79 | # validateCertificates can be ImapConnect::VALIDATE_CERT or ImapConnect::NOVALIDATE_CERT 80 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, 81 | 'secure' => ImapConnect::SECURE, # or null 82 | 'norsh' => ImapConnect::NORSH, # or null 83 | 'readonly' => ImapConnect::READONLY, # or null 84 | 'anonymous' => ImapConnect::ANONYMOUS, # or null 85 | 'debug' => ImapConnect::DEBUG # or null 86 | ], 87 | 'mailbox' => [ 88 | 'remote_system_name' => 'imap.server.ru', 89 | 'port' => null, 90 | 'flags' => null, 91 | 'mailbox_name' => null, 92 | ], 93 | 'connect' => [ 94 | 'mailbox' => null, 95 | 'username' => 'user', 96 | 'password' => 'pass', 97 | 'options' => 0, 98 | 'n_retries' => 0, 99 | 'params' => [], 100 | ] 101 | ]); 102 | ``` 103 | -------------------------------------------------------------------------------- /docs/guide/contributing.md: -------------------------------------------------------------------------------- 1 | # Welcome contributors! 2 | Thanks for wanting to help us and this library out! 3 | # Getting started 4 | Clone this repo locally and make some code changes that better out library. 5 | Or update our docs and help other developers like you ise this library. 6 | If you do not know what git is please see [github's guide to git](https://help.github.com/articles/git-and-github-learning-resources/). 7 | After that make a pull request and fill out the pull request form. A person with correct permission will review the changes. Anwser any 8 | questions and make any needed changes. 9 | 10 | # Thanks! 11 | THANK YOU!!! We cannot say it enough. We love devs helping us out! We are here to help you along the way! 12 | 13 | # Side notes 14 | All pull request *must* follow the proper format. Only a few (from certain people) will be merged without the form. 15 | You must add your name to the credits once you feel that you have made a reasonable impact on our library. 16 | Please, note that when you send us code and changes we then _legally_ own those changes. 17 | There are no take backs. 18 | Please contact a person with proper permissions for issues with this. 19 | -------------------------------------------------------------------------------- /docs/guide/errordb.md: -------------------------------------------------------------------------------- 1 | # Errors 2 | 3 | Many errors may be thrown while using the library, if you cant seem to find what an error means or what you are doing wrong, take a look here. 4 | Everything is structured like this: 5 | #### [Error name] 6 | [How error occurs] 7 | [fix] 8 | #### Imap function not available 9 | PHP does not support connections to web servers via imap 10 | To fix this download the php_imap.dll and enable it by putting the following text in php.ini `extension=php_gettext.dll` 11 | #### Mailbox is not installed 12 | No mail box was provided 13 | Confirm that the $mailbox variable is filled when connecting 14 | #### Mailbox must be a string 15 | The mailbox provided to connect to the web server is not a string 16 | We cannot connect to mail boxes that have a integer in them. Make sure the $mailbox variable provied is a string 17 | #### Username must be a string 18 | The username provided to connect to the web server is not a string 19 | Web servers dont use arrays as usernames!!! Make sure the $username variable is a string 20 | #### Password must be a string 21 | The password provided to connect to the web server is not a string 22 | Confirm that the $password variable is a string 23 | #### Options must be an integer 24 | The options variable provided when connecting is not an integer 25 | // Dont know. Someone checking pr tell me and ill fix 26 | #### N_ must be an integer 27 | The number of retries provided is not an integer 28 | Make sure that the $N_retries variable is an integer 29 | #### Params must be an array 30 | The parameters provided to connect to the server are not an array 31 | // Dont know. Someone checking pr tell me and ill fix 32 | #### Error connecting to[insert your mailbox string here] 33 | PHP-imap client had trouble connecting to the provided mailbox with the provided details 34 | This can mean many things. It can mean your mailbox is invalid or your username and password are not valid. Comfirm your login details and make sure your mail server is online 35 | #### Option connect must be installed 36 | If you selected the advanced connection and not installed `connect` option like 37 | ```php 38 | $imap = new ImapClient([ 39 | 'connect' => [ 40 | 'username' => 'user', 41 | 'password' => 'pass', 42 | ] 43 | ]); 44 | ``` 45 | #### File must be specified for saveEmail() 46 | You did not specify a file path 47 | Insure your code looks like this: 48 | ```php 49 | $imap->saveEmail($your_file_path_var, $your_email_id_var, $your_part_var) 50 | ``` 51 | #### Email id must be specified for saveEmail() 52 | You did not specify an email id 53 | Insure your code looks like this: 54 | ```php 55 | $imap->saveEmail($your_file_path_var, $your_email_id_var, $your_part_var) 56 | ``` 57 | #### File must be a string for saveEmail() 58 | The provided file path is not a string 59 | Make sure your $file is a string *not* a open file 60 | #### $id must be a integer for saveEmail() 61 | The provided email id is an integer 62 | Make sure your $id is an integer 63 | #### What to use id or uid? 64 | You did not let us know weather to use id or uuids 65 | -------------------------------------------------------------------------------- /docs/guide/examples.md: -------------------------------------------------------------------------------- 1 | # Composer 2 | ```php 3 | getMessage().PHP_EOL; 25 | die(); // Oh no :( we failed 26 | } 27 | 28 | // Get all folders as array of strings 29 | $folders = $imap->getFolders(); 30 | foreach($folders as $folder) { 31 | echo $folder; 32 | } 33 | 34 | // Select the folder Inbox 35 | $imap->selectFolder('INBOX'); 36 | 37 | // Count the messages in current folder 38 | $overallMessages = $imap->countMessages(); 39 | $unreadMessages = $imap->countUnreadMessages(); 40 | 41 | // Fetch all the messages in the current folder 42 | $emails = $imap->getMessages(); 43 | var_dump($emails); 44 | 45 | // Create a new folder named "archive" 46 | $imap->addFolder('archive'); 47 | 48 | // Move the first email to our new folder 49 | $imap->moveMessage($emails[0]['uid'], 'archive'); 50 | 51 | // Delete the second message 52 | $imap->deleteMessage($emails[1]['uid']); 53 | ``` 54 | # Connect 55 | ```php 56 | [ 96 | 'service' => ImapConnect::SERVICE_IMAP, 97 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 98 | /* This NOVALIDATE_CERT is used when the server connecting to the imao 99 | * servers is not https but the imap is. This ignores the failure. 100 | */ 101 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, 102 | ], 103 | 'mailbox' => [ 104 | 'remote_system_name' => 'imap.server.ru', 105 | ], 106 | 'connect' => [ 107 | 'username' => 'user', 108 | 'password' => 'pass' 109 | ] 110 | ]); 111 | 112 | /* Example 2 113 | * Get debug messages 114 | */ 115 | $imap = new ImapClient([ 116 | 'flags' => [ 117 | 'service' => ImapConnect::SERVICE_IMAP, 118 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 119 | 'validateCertificates' => ImapConnect::VALIDATE_CERT, 120 | // Turns debug on or off 121 | 'debug' => ImapConnect::DEBUG, 122 | ], 123 | 'mailbox' => [ 124 | 'remote_system_name' => 'imap.server.ru', 125 | 'port' => 123, 126 | ], 127 | 'connect' => [ 128 | 'username' => 'user', 129 | 'password' => 'pass' 130 | ] 131 | ]); 132 | 133 | /* Example 3 134 | * You can also set the config then connects 135 | */ 136 | ImapClient::setConnectAdvanced(); 137 | ImapClient::setConnectConfig([ 138 | 'flags' => [ 139 | 'service' => ImapConnect::SERVICE_POP3, 140 | 'validateCertificates' => ImapConnect::VALIDATE_CERT, 141 | 'debug' => ImapConnect::DEBUG, 142 | ], 143 | ]); 144 | $imap = new ImapClient(); 145 | 146 | /* Example 5 147 | * Here you can see all the options 148 | */ 149 | $imap = new ImapClient([ 150 | 'flags' => [ 151 | 'service' => ImapConnect::SERVICE_IMAP, # ImapConnect::SERVICE_IMAP ,ImapConnect::SERVICE_POP3, ImapConnect::SERVICE_NNTP 152 | 'encrypt' => ImapConnect::ENCRYPT_SSL, # ImapConnect::ENCRYPT_SSL, ImapConnect::ENCRYPT_TLS, ImapConnect::ENCRYPT_NOTLS 153 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, # ImapConnect::VALIDATE_CERT, ImapConnect::NOVALIDATE_CERT 154 | 'secure' => ImapConnect::SECURE, # or null 155 | 'norsh' => ImapConnect::NORSH, # or null 156 | 'readonly' => ImapConnect::READONLY, # or null 157 | 'anonymous' => ImapConnect::ANONYMOUS, # or null 158 | 'debug' => ImapConnect::DEBUG # or null 159 | ], 160 | 'mailbox' => [ 161 | 'remote_system_name' => 'imap.server.ru', 162 | 'port' => null, 163 | 'flags' => null, 164 | 'mailbox_name' => null, 165 | ], 166 | 'connect' => [ 167 | 'mailbox' => null, 168 | 'username' => 'user', 169 | 'password' => 'pass', 170 | 'options' => 0, 171 | 'n_retries' => 0, 172 | 'params' => [], 173 | ] 174 | ]); 175 | ``` 176 | # Direct 177 | ```php 178 | getMessage().PHP_EOL; 201 | die(); 202 | } 203 | 204 | // Get all of the folders as an array of strings 205 | $folders = $imap->getFolders(); 206 | foreach($folders as $folder) { 207 | echo $folder; 208 | } 209 | 210 | // Select the folder named INBOX 211 | $imap->selectFolder('INBOX'); 212 | 213 | // Count the messages in the current folder 214 | $overallMessages = $imap->countMessages(); 215 | $unreadMessages = $imap->countUnreadMessages(); 216 | 217 | // Fetch all of the emails in the current folder 218 | $emails = $imap->getMessages(); 219 | var_dump($emails); 220 | 221 | // Create a new folder named "Archive" 222 | $imap->addFolder('archive'); 223 | 224 | // Move the first email from INBOX to our new folder 225 | $imap->moveMessage($emails[0]['uid'], 'archive'); 226 | 227 | // Delete the second message in INBOX 228 | $imap->deleteMessage($emails[1]['uid']); 229 | ``` 230 | -------------------------------------------------------------------------------- /docs/guide/gettingstarted.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | In order to get started, we first need to get php-imap-client to your code base. 3 | There are currently two ways of doing this. 4 | ###1) Composer 5 | `composer require ssilence/php-imap-client dev-master` 6 | ```php 7 | require_once "vendor/autoload.php"; 8 | ``` 9 | ###2) Manually 10 | 1) Download the files from github or the releases page 11 | 2) Extract the files into the folder you wish 12 | 3) In the file that will call methods add 13 | ```php 14 | require_once "path/to/ImapClientException.php"; 15 | require_once "path/to/ImapConnect.php"; 16 | require_once "path/to/ImapClient.php"; 17 | require_once "path/to/IncomingMessage.php"; 18 | require_once "path/to/TypeAttachments.php"; 19 | require_once "path/to/TypeBody.php"; 20 | ``` 21 | After this, we need to let php we need these classes: 22 | ```php 23 | use SSilence\ImapClient\ImapClientException; 24 | use SSilence\ImapClient\ImapConnect; 25 | use SSilence\ImapClient\ImapClient as Imap; 26 | ``` 27 | Next, we need to declare out variables: 28 | ```php 29 | $mailbox = 'my.imapserver.com'; 30 | $username = 'username'; 31 | $password = 'secret'; 32 | $encryption = Imap::ENCRYPT_SSL; // TLS OR NULL accepted 33 | ``` 34 | Next, we need to open the connection: 35 | ```php 36 | // Open connection 37 | try{ 38 | $imap = new Imap($mailbox, $username, $password, $encryption); 39 | // You can also check out example-connect.php for more connection options 40 | 41 | }catch (ImapClientException $error){ 42 | echo $error->getMessage().PHP_EOL; // You know the rule, no errors in production ... 43 | die(); // Oh no :( we failed 44 | } 45 | ``` 46 | After that we can do all this fun stuff :) 47 | ```php 48 | // Get all folders as array of strings 49 | $folders = $imap->getFolders(); 50 | foreach($folders as $folder) { 51 | echo $folder; 52 | } 53 | 54 | // Select the folder INBOX 55 | $imap->selectFolder('INBOX'); 56 | 57 | // Count the messages in current folder 58 | $overallMessages = $imap->countMessages(); 59 | $unreadMessages = $imap->countUnreadMessages(); 60 | 61 | // Fetch all the messages in the current folder 62 | $emails = $imap->getMessages(); 63 | var_dump($emails); 64 | 65 | // Create a new folder named "archive" 66 | $imap->addFolder('archive'); 67 | 68 | // Move the first email to our new folder 69 | $imap->moveMessage($emails[0]['uid'], 'archive'); 70 | 71 | // Delete the second message 72 | $imap->deleteMessage($emails[1]['uid']); 73 | ``` 74 | 75 | -------------------------------------------------------------------------------- /docs/guide/incomingmessage.md: -------------------------------------------------------------------------------- 1 | # Incoming Message 2 | 3 | ## Structure 4 | Incoming Message is an object(SSilence\ImapClient\IncomingMessage) that has 5 basic properties 5 | ``` 6 | $imap->incomingMessage->header 7 | $imap->incomingMessage->message 8 | $imap->incomingMessage->attachments 9 | $imap->incomingMessage->section 10 | $imap->incomingMessage->structure 11 | ``` 12 | and one for debugging 13 | ``` 14 | $imap->incomingMessage->debug 15 | ``` 16 | 17 | ### Header properties 18 | The header properties typically look like this: 19 | ``` 20 | ["header"]=> 21 | object(stdClass)#3 (15) { 22 | ["subject"]=> 23 | string(10) "Ocoto cat is in the new!" 24 | ["from"]=> 25 | string(36) "..." 26 | ["to"]=> 27 | string(36) "..." 28 | ["date"]=> 29 | string(31) "Tue, 21 Mar 2017 11:02:37 +0000" 30 | ["message_id"]=> 31 | string(37) "<1490094157.905281607@f369.i.gmail.com>" 32 | ["size"]=> 33 | int(472418) 34 | ["uid"]=> 35 | int(42243) 36 | ["msgno"]=> 37 | int(82) 38 | ["recent"]=> 39 | int(0) 40 | ["flagged"]=> 41 | int(0) 42 | ["answered"]=> 43 | int(0) 44 | ["deleted"]=> 45 | int(0) 46 | ["seen"]=> 47 | int(1) 48 | ["draft"]=> 49 | int(0) 50 | ["udate"]=> 51 | int(1490094157) 52 | ["details"]=> 53 | object(stdClass)#4 (20) {} 54 | } 55 | ``` 56 | To get the subject of the message, use 57 | $imap->incomingMessage->header->subject 58 | 59 | This object also contains the id and the uid of the current message according to https://php.net/manual/en/function.imap-fetch-overview.php: 60 | ```php 61 | # id 62 | $imap->incomingMessage->header->msgno; 63 | # uid 64 | $imap->incomingMessage->header->uid; 65 | ``` 66 | 67 | To get the CC or BCC use the property 68 | ```php 69 | $imap->incomingMessage->header->details 70 | ``` 71 | Header details properties have more properties like this [imap_headerinfo](https://php.net/manual/en/function.imap-headerinfo.php). 72 | If there is no property in the returned object, then it is not present in this email. 73 | To get the CC or BCC of the message, use 74 | ```php 75 | $imap->incomingMessage->header->details->cc 76 | # or 77 | $imap->incomingMessage->header->details->bcc 78 | # cc and bcc is an array of objects with properties [personal, adl, mailbox, host] 79 | ``` 80 | 81 | ### Message properties 82 | Message properties will look like this: 83 | ``` 84 | ["message"]=> 85 | object(stdClass)#29 (3) { 86 | ["plain"]=> 87 | string(16) "text message" 88 | ["text"]=> 89 | string(16) "text message" 90 | ["info"]=> 91 | array(2) { 92 | [0]=> 93 | object(stdClass)#27 (2) { 94 | ["structure"]=> 95 | object(stdClass)#28 (11) { 96 | ["type"]=> 97 | int(0) 98 | ["encoding"]=> 99 | int(3) 100 | ["ifsubtype"]=> 101 | int(1) 102 | ["subtype"]=> 103 | string(5) "PLAIN" 104 | ["ifdescription"]=> 105 | int(0) 106 | ["ifid"]=> 107 | int(0) 108 | ["bytes"]=> 109 | int(20) 110 | ["ifdisposition"]=> 111 | int(0) 112 | ["ifdparameters"]=> 113 | int(0) 114 | ["ifparameters"]=> 115 | int(1) 116 | ["parameters"]=> 117 | array(1) { 118 | [0]=> 119 | object(stdClass)#33 (2) { 120 | ["attribute"]=> 121 | string(7) "charset" 122 | ["value"]=> 123 | string(5) "utf-8" 124 | } 125 | } 126 | } 127 | ["body"]=> 128 | string(16) "text message" 129 | } 130 | [1]=> 131 | object(stdClass)#30 (2) { 132 | ["structure"]=> 133 | object(stdClass)#31 (11) { 134 | ["type"]=> 135 | int(0) 136 | ["encoding"]=> 137 | int(3) 138 | ["ifsubtype"]=> 139 | int(1) 140 | ["subtype"]=> 141 | string(4) "HTML" 142 | ["ifdescription"]=> 143 | int(0) 144 | ["ifid"]=> 145 | int(0) 146 | ["bytes"]=> 147 | int(72) 148 | ["ifdisposition"]=> 149 | int(0) 150 | ["ifdparameters"]=> 151 | int(0) 152 | ["ifparameters"]=> 153 | int(1) 154 | ["parameters"]=> 155 | array(1) { 156 | [0]=> 157 | object(stdClass)#34 (2) { 158 | ["attribute"]=> 159 | string(7) "charset" 160 | ["value"]=> 161 | string(5) "utf-8" 162 | } 163 | } 164 | } 165 | ["body"]=> 166 | string(56) "


html text message
" 167 | } 168 | } 169 | ["html"]=> 170 | string(56) "


html text message
" 171 | } 172 | ``` 173 | To get info on the message use 174 | $imap->incomingMessage->message->info Is an array. It will allways return an array 175 | 176 | To get the body of the message use 177 | ```php 178 | $imap->incomingMessage->message->html 179 | # or 180 | $imap->incomingMessage->message->plain 181 | # or synonym plain 182 | $imap->incomingMessage->message->text 183 | ``` 184 | Get the emails charset like this: 185 | ```php 186 | $imap->incomingMessage->message->html->charset 187 | ``` 188 | Get all subtype message: 189 | ```php 190 | # array 191 | $imap->incomingMessage->message->types 192 | ``` 193 | These are auto generated by the client. This allows you to have an html and a plain text version of the email 194 | 195 | ### Attachments properties 196 | Attachment properties is an array of objects. 197 | If the letter has attachments, then its return will look like as follows 198 | ``` 199 | ["attachments"]=> 200 | array(1) { 201 | [0]=> 202 | object(stdClass)#26 (3) { 203 | ["name"]=> string(19) "20140228_160524.jpg" 204 | ["body"]=> string(945576) "..." 205 | ["info"]=> "info about attachment" 206 | 207 | ... and next object ... 208 | [1]=> 209 | object(stdClass)#18 (2) {} 210 | } 211 | ``` 212 | 213 | The attachment object has 3 basic properties. Like this: 214 | ```php 215 | $imap->incomingMessage->attachments[0]->name; 216 | $imap->incomingMessage->attachments[0]->body; 217 | $imap->incomingMessage->attachments[0]->info; 218 | ``` 219 | 220 | To get the names of all attachments in an email, use the following 221 | ```php 222 | foreach($attachments as $attachment){ 223 | echo $attachment->name; 224 | } 225 | ``` 226 | 227 | All information for the email will be in the info property. It will look like this: 228 | ```php 229 | ["info"]=> 230 | object(stdClass)#18 (2) { 231 | ["structure"]=> 232 | object(stdClass)#19 (13) { 233 | ["type"]=> 234 | int(5) 235 | ["encoding"]=> 236 | int(3) 237 | ["ifsubtype"]=> 238 | int(1) 239 | ["subtype"]=> 240 | string(4) "JPEG" 241 | ["ifdescription"]=> 242 | int(0) 243 | ["ifid"]=> 244 | int(0) 245 | ["bytes"]=> 246 | int(237916) 247 | ["ifdisposition"]=> 248 | int(1) 249 | ["disposition"]=> 250 | string(10) "attachment" 251 | ["ifdparameters"]=> 252 | int(1) 253 | ["dparameters"]=> 254 | array(1) { 255 | [0]=> 256 | object(stdClass)#25 (2) { 257 | ["attribute"]=> 258 | string(8) "filename" 259 | ["value"]=> 260 | string(12) "Location.jpg" 261 | } 262 | } 263 | ["ifparameters"]=> 264 | int(1) 265 | ["parameters"]=> 266 | array(1) { 267 | [0]=> 268 | object(stdClass)#24 (2) { 269 | ["attribute"]=> 270 | string(4) "name" 271 | ["value"]=> 272 | string(12) "Location.jpg" 273 | } 274 | } 275 | } 276 | ["body"]=>string(173862) "" 277 | ``` 278 | 279 | The attachment info object has 2 basic properties. 280 | ```php 281 | $imap->incomingMessage->attachment[0]->info->structure 282 | $imap->incomingMessage->attachment[0]->info->body 283 | ``` 284 | 285 | Body properties contain the body attachment(s). 286 | 287 | ### Section properties 288 | Section properties it is array sections of which the email consists. 289 | ``` 290 | ["section"]=> 291 | array(5) { 292 | [0]=> 293 | string(1) "1" 294 | [1]=> 295 | string(1) "2" 296 | [2]=> 297 | string(1) "3" 298 | [5]=> 299 | string(3) "1.1" 300 | [7]=> 301 | string(3) "1.2" 302 | } 303 | ``` 304 | ### Structure properties 305 | The structure propertie is the complete structure of the email 306 | ### Debug properties 307 | The debug propertie can be used for debugging 308 | -------------------------------------------------------------------------------- /docs/guide/install.md: -------------------------------------------------------------------------------- 1 | # Installing 2 | PHP-imap-client can be installed 2 ways. The first composer and the second manual 3 | ### 1) Composer 4 | Untill Big o' 2.0 is ready, use the following command to install PHP-imap-library: 5 | `composer require ssilence/php-imap-client dev-master` 6 | ### 2) Manual 7 | 1) Download the files from github or the releases page 8 | 2) Extract the files into the folder you wish 9 | 3) In the file that will call methods add 10 | ```php 11 | require_once "path/to/ImapClientException.php"; 12 | require_once "path/to/ImapConnect.php"; 13 | require_once "path/to/ImapClient.php"; 14 | require_once "path/to/IncomingMessage.php"; 15 | require_once "path/to/TypeAttachments.php"; 16 | require_once "path/to/TypeBody.php"; 17 | ``` 18 | You may then use connect etc 19 | -------------------------------------------------------------------------------- /docs/guide/methods.md: -------------------------------------------------------------------------------- 1 | # Methods 2 | 3 | The following methods are currently available. 4 | 5 | * ``__construct($mailbox, $username, $password, $encryption)`` open new imap connection 6 | * ``isConnected()`` check whether the imap connection was opened successfully 7 | * ``getError()`` returns the last error message. 8 | * ``getFolders($separator, $type)`` @param string $separator. Default is '.' @param int $type. Has three meanings 0,1,2. If 0 returns a nested array, if 1 it returns an array of strings, if 2 returns raw data from imap_list(). 9 | * ``getMessage($id)`` get email by given id. 10 | * ``getMessages($number, $start, $order)`` get emails in current folder. 11 | * ``getUnreadMessages($read)`` get unread messages in current folder and mark them read. Use $read = false marks them unread. 12 | * ``getQuota($user)`` Retrieve the quota level settings, and usage statics per mailbox. 13 | * ``getQuotaRoot($user)`` Retrieve the quota level settings, and usage statics per mailbox. 14 | * ``getAllEmailAddresses()`` returns all email addresses of all emails (for auto suggestion list). 15 | * ``getMailboxStatistics()`` returns statistics, see [imap_mailboxmsginfo](https://php.net/manual/en/function.imap-mailboxmsginfo.php). 16 | * ``getHeaderInfo($msgNumber)`` Get the header info via the message number. https://php.net/manual/en/function.imap-headerinfo.php#refsect1-function.imap-headerinfo-returnvalues 17 | * ``getMessagesByCriteria($criteria, $number, $start, $order)`` Get messages by criteria like 'FROM uncle'. 18 | * ``getBriefInfoMessages()`` Get a short information about the messages in the current folder. 19 | * ``getSection($id, $section)`` Get the section of the specified message. 20 | * ``getUid($id)`` Get uid through id. 21 | * ``getId($uid)`` Get id through uid. 22 | * ``setUnseenMessage($id, $seen = true)`` set unseen state of the message with given id. 23 | * ``setEmbed($val)`` If true, embed all 'inline' images into body HTML, accesible in 'body_embed'. 24 | * ``setEncoding()`` Identify encoding by charset attribute in header. 25 | * ``selectFolder($folder)`` select the provided folder. 26 | * ``countMessages()`` count the messages in current folder. 27 | * ``countUnreadMessages()`` count the unread messages in current folder. 28 | * ``deleteMessage($id)`` delete message with given id. 29 | * ``deleteMessages($ids)`` delete messages with given ids (as array). 30 | * ``moveMessage($id, $target)`` move message with given id in new folder 31 | * ``moveMessages($ids, $target)`` move messages with given ids (as array) in new folder 32 | * ``addFolder($name)`` add new folder with given name 33 | * ``removeFolder($name)`` delete folder with given name 34 | * ``renameFolder($name, $newname)`` rename folder with given name 35 | * ``purge()`` move all emails in the current folder into trash. emails in trash and spam will be deleted. 36 | * ``convertToUtf8()`` Apply encoding defined in header 37 | * ``saveMessageInSent($header, $body)`` save a sent message in sent folder 38 | * ``saveEmail($file , $id, $part)`` saves an email to the $file file 39 | * ``saveEmailSafe($file , $id, $part, $streamFilter)`` saves an email to the $file file. This is recommended for servers with low amounts of RAM. Stream filter is set to convert.base64-decode by default. 40 | * ``saveAttachments($options)`` Save attachments one incoming message. You can set any of the options: ``$options['dir'=>null, 'incomingMessage'=>null]``. 41 | * ``saveAttachmentsMessagesBySubject($subject, $dir = null, $charset = null)`` Save Attachmets Messages By Subject 42 | * ``sendMail()`` Send a message using the adapter. 43 | -------------------------------------------------------------------------------- /docs/guide/outgoing.md: -------------------------------------------------------------------------------- 1 | # Adapter for outgoing messages 2 | 3 | The IMAP protocol is designed to read messages and does not support the sending of messages. 4 | But if you want ImapClient to send messages, then use the AdapterForOutgoingMessage class. 5 | To do this you will also need to install PHPMailer 6 | 7 | ## First step 8 | The first step is to install php mailer. You can do so in two ways. 9 | 10 | 1) Composer 11 | ```php 12 | composer require phpmailer/phpmailer 13 | ``` 14 | 2) Minimal 15 | Read [PHPMailer's guide on a minimal installation](https://github.com/PHPMailer/PHPMailer#minimal-installation) 16 | 17 | ## Second step 18 | Turn phpmailer into an adapter. 19 | File AdapterForOutgoingMessage.php 20 | To do this, we rewrite send() method. 21 | Note this is note safe yet and may be re written in the future. 22 | ```php 23 | # Be sure to add a namespace 24 | use \PHPMailer; 25 | 26 | class AdapterForOutgoingMessage 27 | { 28 | # ... code ... 29 | 30 | public function send() 31 | { 32 | $mail = new PHPMailer; 33 | $mail->isSMTP(); 34 | $mail->Host = 'smtp1.example.com;smtp2.example.com'; 35 | $mail->SMTPAuth = true; 36 | $mail->Username = $this->config['connect']['username']; 37 | $mail->Password = $this->config['connect']['password']; 38 | $mail->SMTPSecure = 'tls'; 39 | $mail->Port = 587; 40 | $mail->setFrom(self::$options['fromEmail'], self::$options['fromEmailName']); 41 | $mail->addAddress(self::$options['toEmail'], self::$options['toEmailName']); 42 | $mail->addAttachment(self::$options['fileName']); 43 | $mail->isHTML(true); 44 | $mail->Subject = self::$options['subject']; 45 | $mail->Body = self::$options['messageHtml']; 46 | $mail->AltBody = self::$options['messagePlain']; 47 | if(!$mail->send()) { 48 | throw new ImapClientException('Message could not be sent'.PHP_EOL.$mail->ErrorInfo); 49 | } else { 50 | # echo 'Message has been sent'; 51 | return true; 52 | }; 53 | return false; 54 | } 55 | 56 | # ... code ... 57 | } 58 | ``` 59 | 60 | ## Third step 61 | Now you can send messages like this: 62 | ```php 63 | 64 | use SSilence\ImapClient\AdapterForOutgoingMessage; 65 | 66 | try{ 67 | 68 | $imap = new ImapClient([ 69 | 'flags' => [ ... ], 70 | 'mailbox' => [ ... ], 71 | 'connect' => [ 72 | 'username' => 'user@gmail.com', 73 | 'password' => 'password', 74 | ] 75 | ]); 76 | 77 | # ... code ... 78 | 79 | AdapterForOutgoingMessage::setOptions([ 80 | 'fromEmail' => 'from@gmail.com', 81 | 'fromEmailName' => 'fromUser', 82 | 'toEmail' => 'to@gmail.com', 83 | 'toEmailName' => 'toUser', 84 | 'fileName' => 'file', 85 | 'subject' => 'subject', 86 | 'messageHtml' => 'message html', 87 | 'messagePlain' => 'message' 88 | ]); 89 | $imap->sendMail(); 90 | 91 | # ... code ... 92 | 93 | }catch (ImapClientException $error){ 94 | echo $error->getInfo(); 95 | }; 96 | ``` -------------------------------------------------------------------------------- /docs/guide/usage.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | #### After install prep 3 | After you install this library ensure you have added the required classes. 4 | A basic connection may look like this: 5 | ```php 6 | $mailbox = 'my.imapserver.com'; 7 | $username = 'myuser'; 8 | $password = 'secret'; 9 | $encryption = Imap::ENCRYPT_SSL; // or ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS or null 10 | 11 | try{ 12 | 13 | $imap = new Imap($mailbox, $username, $password, $encryption); 14 | # ... and further code ... 15 | 16 | }catch (ImapClientException $error){ 17 | echo $error->getInfo(); 18 | }; 19 | ``` 20 | The above code connects you to a mail server and makes sure it connected. Change the variables to your information 21 | #### After connection 22 | There are many things you can do after the code above. 23 | For example you can get and echo all folders 24 | ```php 25 | $folders = $imap->getFolders(); 26 | var_dump($folders); 27 | # and 28 | foreach($folders as $folder) { 29 | echo $folder; 30 | } 31 | # or 32 | foreach($folders as $folder => $subFolder) { 33 | echo $folder.PHP_EOL; 34 | echo $subFolder.PHP_EOL; 35 | } 36 | ``` 37 | See [getFolders()](Methods.md) method settings. 38 | You can also select folders 39 | 40 | ```php 41 | $imap->selectFolder("Inbox"); 42 | ``` 43 | Once you selected a folder you can count the number of messages in the folder: 44 | 45 | ```php 46 | $overallMessages = $imap->countMessages(); 47 | $unreadMessages = $imap->countUnreadMessages(); 48 | ``` 49 | 50 | To get a brief summary of all messages in the current folder, including the message ID you can use this 51 | ```php 52 | $imap->getBriefInfoMessages() 53 | ``` 54 | 55 | Get the message with ID 82 56 | ```php 57 | $imap->getMessage(82) 58 | ``` 59 | 60 | Save all of the attachmets in this email. 61 | ```php 62 | $imap->saveAttachments(); 63 | ``` 64 | or 65 | ```php 66 | $imap->saveAttachments(['dir'=>'dir/to/save']); 67 | ``` 68 | or save the attachment(s) like this 69 | ```php 70 | $message = $imap->getMessage(82); 71 | $imap->saveAttachments(['dir' => "your/path/", 'incomingMessage' => $message]); 72 | ``` 73 | 74 | It is also possible to save all attachments for messages with a special word in the message subject. 75 | ```php 76 | $imap->saveAttachmetsMessagesBySubject('Special text', 'path/to/save/attach'); 77 | ``` 78 | 79 | Get the header info like cc and bcc 80 | ```php 81 | var_dump(getHeaderInfo(1)); 82 | ``` 83 | 84 | Get all unread messages. 85 | ```php 86 | $imap->getUnreadMessages() 87 | ``` 88 | 89 | Okay, now lets fetch all emails in the currently selected folder (in our example the "Inbox"): 90 | ```php 91 | $emails = $imap->getMessages(); 92 | ``` 93 | Now $emails it is array object. 94 | 95 | The structure of a single message when it is received by the method getMessage() or getMessages() 96 | it by the look here [Incoming Message](IncomingMessage.md) 97 | 98 | For example get subject and simple text messages 99 | ```php 100 | foreach($emails as $email){ 101 | echo $email->header->subject.PHP_EOL; 102 | echo $email->message->plain.PHP_EOL; 103 | }; 104 | ``` 105 | 106 | You can also add/rename/delete folders. Lets add a new folder: 107 | 108 | ```php 109 | $imap->addFolder('archive'); 110 | ``` 111 | Now we move the first email into this folder 112 | 113 | ```php 114 | $imap->moveMessage($emails[0]['id'], 'archive'); 115 | ``` 116 | And we delete the second email from inbox 117 | 118 | ```php 119 | $imap->deleteMessage($emails[1]['id']); 120 | ``` 121 | 122 | We also can save emails 123 | ```php 124 | // Note: for slower web servers will less ram use saveEmailSafe() 125 | $imap->saveEmail('archive/users/johndoe/email_1.eml', 1); 126 | ``` 127 | 128 | You can use the method of sending messages. 129 | ```php 130 | $imap->sendMail(); 131 | ``` 132 | But for this you need to take several steps. 133 | [Adapter for outgoing message. Use in 3 steps.](AdapterForOutgoingMessage.md) 134 | 135 | For a full list of methods you can do check [current list of methods](Methods.md). 136 | 137 | #### Advanced connecting 138 | 139 | You can also use the below code to add some more options while connecting 140 | 141 | ```php 142 | $imap = new ImapClient([ 143 | 'flags' => [ 144 | 'service' => ImapConnect::SERVICE_IMAP, # ImapConnect::SERVICE_IMAP ,ImapConnect::SERVICE_POP3, ImapConnect::SERVICE_NNTP 145 | 'encrypt' => ImapConnect::ENCRYPT_SSL, # ImapConnect::ENCRYPT_SSL, ImapConnect::ENCRYPT_TLS, ImapConnect::ENCRYPT_NOTLS 146 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, # ImapConnect::VALIDATE_CERT, ImapConnect::NOVALIDATE_CERT 147 | # ... and other 148 | ], 149 | 'mailbox' => [ 150 | 'remote_system_name' => 'imap.server.ru', 151 | 'port' => '431', 152 | 'mailbox_name' => 'INBOX.Send', 153 | # ... and other 154 | ], 155 | 'connect' => [ 156 | 'username' => 'user', 157 | 'password' => 'pass', 158 | # ... and other 159 | ] 160 | ]); 161 | ``` 162 | All connecting options you can see in example-connect.php file 163 | or go [Advanced connecting](AdvancedConnecting.md) 164 | or you can see code ImapConnect class. 165 | -------------------------------------------------------------------------------- /docs/incomingmessage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/installing.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/methods.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/outgoing.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /docs/phpImapClient.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SSilence/php-imap-client/3758ae894a961346c44dd289f32071337ae9e9f7/docs/phpImapClient.zip -------------------------------------------------------------------------------- /docs/usage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | php-imap-client 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 |
34 |
35 |

php-imap-client

36 | 50 |
51 |
52 | 53 | 54 |
55 |
56 | 57 |
58 | 62 |
63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /runtests.php: -------------------------------------------------------------------------------- 1 | getFolders(null, 2); 111 | $find = false; 112 | 113 | foreach ($folders as $folder) { 114 | $pos = strpos($folder, $testFolder); 115 | if($pos !== false) { 116 | $find = true; 117 | }; 118 | }; 119 | 120 | if($find){ 121 | $imap->selectFolder($testFolder); 122 | }else{ 123 | $imap->addFolder($testFolder); 124 | $imap->selectFolder($testFolder); 125 | }; 126 | 127 | $countMessages4 = $imap->countMessages(); 128 | if($countMessages4 == 0){ 129 | echo 'The mailbox is cleared'.PHP_EOL; 130 | }else{ 131 | throw new ImapClientException('The mailbox is not cleaned'); 132 | }; 133 | echo 'Test_2 OK'.PHP_EOL; 134 | 135 | /* 136 | * Test_3 137 | * Send emails in current mailbox and delete 138 | */ 139 | $mailbox = $imap->getMailbox(); 140 | MessagesPool::push(new SimpleMessage()); 141 | MessagesPool::push(new SimpleMessage()); 142 | MessagesPool::push(new SimpleMessage()); 143 | MessagesPool::send($imap->getImap(), $mailbox.$testFolder); 144 | 145 | $countMessages5 = $imap->countMessages(); 146 | if($countMessages5 != 0){ 147 | echo 'Mail(s) is send'.PHP_EOL; 148 | }else{ 149 | throw new ImapClientException('Mail(s) are not sent'); 150 | }; 151 | 152 | $infos = $imap->getBriefInfoMessages(); 153 | $countMessages1 = count($infos); 154 | $countMessages2 = $imap->countMessages(); 155 | 156 | for($i=1; $i<=$countMessages2; $i++) { 157 | $uid = $imap->getUid(1); 158 | $imap->deleteMessage($uid); 159 | }; 160 | 161 | $countMessages3 = $imap->countMessages(); 162 | if($countMessages3 == 0){ 163 | echo 'The mailbox is cleared.'.PHP_EOL; 164 | }else{ 165 | throw new ImapClientException('The mailbox is not cleaned'); 166 | }; 167 | echo 'Test_3 OK'.PHP_EOL; 168 | 169 | /* 170 | * Test_4 171 | * Send email in current mailbox and get it 172 | */ 173 | MessagesPool::clean(); 174 | MessagesPool::push(new SimpleMessage()); 175 | MessagesPool::send($imap->getImap(), $mailbox.$testFolder); 176 | $imap->getMessage(1); 177 | Check::incomingMessage($imap->incomingMessage); 178 | $id = $imap->getUid('1'); 179 | $imap->deleteMessage($id); 180 | MessagesPool::clean(); 181 | echo 'Test_4 OK'.PHP_EOL; 182 | 183 | /* 184 | * Test_5 185 | * Check getMessages() 186 | */ 187 | MessagesPool::clean(); 188 | MessagesPool::push(new SimpleMessage()); 189 | MessagesPool::push(new SimpleMessage()); 190 | MessagesPool::send($imap->getImap(), $mailbox.$testFolder); 191 | $array = $imap->getMessages(); 192 | if(!is_array($array)){ 193 | throw new ImapClientException('getMessages() returns not an array'); 194 | }; 195 | if(count($array) != 2){ 196 | throw new ImapClientException('getMessages() returns an unset number of messages'); 197 | }; 198 | $id = $imap->getUid('1'); 199 | $imap->deleteMessage($id); 200 | $id = $imap->getUid('1'); 201 | $imap->deleteMessage($id); 202 | MessagesPool::clean(); 203 | echo 'Test_5 OK'.PHP_EOL; 204 | 205 | /* 206 | * Test_6 207 | * Check attachment 208 | */ 209 | MessagesPool::push(new TestMessage1()); 210 | MessagesPool::send($imap->getImap(), $mailbox.$testFolder); 211 | $imap->getMessage(1); 212 | var_dump($imap->incomingMessage); 213 | Check::incomingMessage($imap->incomingMessage); 214 | $count = count($imap->incomingMessage->attachments); 215 | if($count == 0){ 216 | throw new ImapClientException('Attachments was not found'); 217 | }; 218 | $id = $imap->getUid('1'); 219 | $imap->deleteMessage($id); 220 | MessagesPool::clean(); 221 | echo 'Test_6 OK'.PHP_EOL; 222 | 223 | }catch (ImapClientException $error){ 224 | echo $error->getInfo(); 225 | }; 226 | -------------------------------------------------------------------------------- /tests/Check.php: -------------------------------------------------------------------------------- 1 | header)){ 30 | throw new ImapClientException('incomingMessage->header not installed.'); 31 | }; 32 | if(!isset($incomingMessage->message)){ 33 | throw new ImapClientException('incomingMessage->message not installed.'); 34 | }; 35 | if(!isset($incomingMessage->attachments)){ 36 | throw new ImapClientException('incomingMessage->attachments not installed.'); 37 | }; 38 | if(!isset($incomingMessage->section)){ 39 | throw new ImapClientException('incomingMessage->section not installed.'); 40 | }; 41 | if(!isset($incomingMessage->structure)){ 42 | throw new ImapClientException('incomingMessage->structure not installed.'); 43 | }; 44 | } 45 | } -------------------------------------------------------------------------------- /tests/Message.php: -------------------------------------------------------------------------------- 1 | body); 23 | if(!$status){ 24 | throw new ImapClientException('Test message not send in test folder. imap_append() failed'); 25 | }; 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /tests/MessageInterface.php: -------------------------------------------------------------------------------- 1 | send($stream, $folder); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tests/SimpleMessage.php: -------------------------------------------------------------------------------- 1 | body = 17 | "From: meS@example.com\r\n" 18 | ."To: youS@example.com\r\n" 19 | ."Subject: testS\r\n" 20 | ."\r\n" 21 | ."TestS message\r\n"; 22 | } 23 | } -------------------------------------------------------------------------------- /tests/TestImapClient.php: -------------------------------------------------------------------------------- 1 | imap; 19 | } 20 | 21 | public function getMailbox() 22 | { 23 | return $this->mailbox; 24 | } 25 | } -------------------------------------------------------------------------------- /tests/TestMessage1.php: -------------------------------------------------------------------------------- 1 | body = imap_mail_compose($envelope, $body); 47 | #$this->body = nl2br(imap_mail_compose($envelope, $body)); 48 | } 49 | } -------------------------------------------------------------------------------- /tests/config_example.php: -------------------------------------------------------------------------------- 1 | [ 12 | 'service' => ImapConnect::SERVICE_IMAP, 13 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 14 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, 15 | ], 16 | 'mailbox' => [ 17 | 'remote_system_name' => 'imap.server.com', 18 | 'mailbox_name' => 'INBOX', 19 | ], 20 | 'connect' => [ 21 | 'username' => '', 22 | 'password' => '', 23 | ] 24 | ]; -------------------------------------------------------------------------------- /tests/template.php: -------------------------------------------------------------------------------- 1 | [ 24 | 'service' => ImapConnect::SERVICE_IMAP, 25 | 'encrypt' => ImapConnect::ENCRYPT_SSL, 26 | 'validateCertificates' => ImapConnect::NOVALIDATE_CERT, 27 | ], 28 | 'mailbox' => [ 29 | 'remote_system_name' => '', 30 | 'mailbox_name' => '', 31 | ], 32 | 'connect' => [ 33 | 'username' => '', 34 | 'password' => '' 35 | ] 36 | ]); 37 | 38 | 39 | 40 | }catch (ImapClientException $error){ 41 | echo $error->getInfo(); 42 | }; 43 | --------------------------------------------------------------------------------