ImapMailReader.php 1.25 KB
<?php
/**
 * Created by PhpStorm.
 * User: Tsurkanov
 * Date: 02.11.2015
 * Time: 16:56
 */

namespace common\components\mail;


class ImapMailReader extends  MailReader {

    function __construct( $hostname, $username, $password )
    {
        parent::__construct($hostname, $username, $password);
        $this->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 );
    }


}