class imap { private $server = ''; private $username = ''; private $password = ''; private $port = 143; private $type = 'IMAP'; private $ssl = true; /** * @var resource $stream */ private $stream = NULL; public function getEmails( $onlyUnread = false ) { $ssl = ''; if( $this->ssl ) { $ssl = '/ssl'; } $mailbox = sprintf( "{%s:%d/%s%s/novalidate-cert}INBOX", $this->server, $this->port, $this->type, $ssl ); $this->stream = imap_open( $mailbox, $this->username, $this->password ); $MC = imap_check( $this->stream ); $emails = imap_fetch_overview( $this->stream, "1:{$MC->Nmsgs}", 0 ); if( $onlyUnread ) // nur ungelesene E-Mails { foreach( $emails as $key=>$email ) { if( $email->seen ) // Email wurde bereits gelesen { unset( $emails[$key] ); // Email aus Liste entfernen } } } return $emails; } public function getEmailBody( $uid ) { $body = imap_fetchbody( $this->stream, $uid, 1 ); return imap_qprint( $body ); } /** * @param string $uid * @return bool|string */ public function getAttachments( $uid ) { $structure = imap_fetchstructure( $this->stream, $uid ); foreach( $structure->parts as $key=>$part ) { if( isset( $part->disposition ) && $part->disposition === 'attachment' ) { $attachment = imap_fetchbody( $this->stream, $uid, $key+1, FT_INTERNAL ); return str_replace( '=0A=', '', $attachment ); // Zeilenumbrueche } } return false; } public function close() { if( $this->stream !== false ) { imap_close( $this->stream ); } } }