connection = imap_open($hostname, $username, $password); if ($this->connection === false) throw new \Exception('Cannot connect to mail: ' . imap_last_error()); } public function getEmails( $flag ) { return imap_search( $this->connection, $flag ); } public function getEmailBody( $email_number, $section ) { return imap_fetchbody($this->connection, $email_number, $section ); } /** * @return mixed */ public function getListMailboxes() { return imap_list($this->connection, $this->hostname, "*"); } public function getCurrentEmailStructure( $email_number ) { return imap_fetchstructure($this->connection, $email_number); } public function reOpen( $hostname ) { imap_reopen( $this->connection, $hostname ); } function __destruct() { /* close the connection */ imap_close( $this->connection ); } }