Как мне реализовать этот специфический PHP-класс emailReader - PullRequest
0 голосов
/ 04 декабря 2011

класс представлен здесь:

    <?php

    /**
    * EmailReader
    *
    * Access email from an IMAP or POP account.
    * Currenly only supports fetching attachments.
    *
    * @see http://github.com/driverdan/emailreader
    * @see http://driverdan.com
    *
    * @author Dan DeFelippi <dan@driverdan.com>
    * @license MIT
    * @todo Add additional features beyond saving attachments.
    */
class EmailReader {
// Stores mailbox stream
private $mbox;

// Email part types. Accessed via array position, do not reorder.
var $partTypes = array(
    "text",
    "multipart",
    "message",
    "application",
    "audio",
    "image",
    "video",
    "other",
);

/**
 * Constructor opens connection to the server.
 *
 * @see http://www.php.net/manual/en/function.imap-open.php
 *
 * @param string $host Host connection string.
 * @param string $user Username
 * @param string $password Password
 *
 * @return bool Returns true on success, false on failure.
 */
function __construct($host, $user, $password) {
    return (bool)($this->mbox = imap_open($host, $user, $password));
}

/**
 * Destructor closes server connection.
 */
function __destruct() {
    imap_close($this->mbox);
}

/**
 * Decodes a message based on encoding type.
 *
 * @param string $message Email message part.
 * @param int $encoding Encoding type.
 */
function decode($message, $encoding) {
    switch ($encoding) {
        case 0:
        case 1:
            $message = imap_8bit($message);
        break;

        case 2:
            $message = imap_binary($message);
        break;

        case 3:
        case 5:
            $message = imap_base64($message);
        break;

        case 4:
            $message = imap_qprint($message);
        break;
    }

    return $message;
}

/**
 * Saves all email attachments for all emails. Uses original filenames.
 *
 * @todo Handle duplicate filenames.
 *
 * @param string $path Directory path to save files in.
 * @param bool $inline Save inline files (eg photos). Default is true.
 * @param bool $delete Delete all emails after processing. Default is true.
 */
function saveAttachments($path, $inline = true, $delete = true) {
    $numMessages = $this->getNumMessages();

    // Append slash to path if missing
    if ($path[strlen($path) - 1] != '/') {
        $path .= '/';
    }

    // Loop through all messages
    for ($msgId = 1; $msgId <= $numMessages; $msgId++) {
        $structure = imap_fetchstructure($this->mbox, $msgId, FT_UID);    
        $fileNum = 2;

        // Loop through all email parts
        foreach ($structure->parts as $part) {
            // Handle attachments and inline files (images)
            if (strtoupper($part->disposition) == "ATTACHMENT" || ($inline && strtoupper($part->disposition) == "INLINE")) {
                /**
                 * File extension is determined first by MIME type.
                 * This is because some phone email clients do not use real filenames for attachments.
                 * Other phones use CONTENT-OCTET or other generic MIME type so fallback to file extension.
                 * This was designed to process images so it may not work correctly for some MIME types.
                 */
                $ext = strtolower($part->subtype);

                if (strlen($ext) > 4) {
                    $ext = end(explode('.', $part->dparameters[0]->value));
                } else if ($ext == "jpeg") {
                    $ext = "jpg";
                }
                // @TODO Add other MIME types here?

                $filename = $entry['id'] . ".$ext";

                // Get the body and decode it
                $body = imap_fetchbody($this->mbox, $msgId, $fileNum);
                $data = self::decode($body, $part->type);

                // Save the file
                $fp = fopen("$path$filename", "w");
                fputs($fp, $data);
                fclose($fp);

                $fileNum++;
            }
        }

        if ($delete) {
            $this->delete($msgId);
        }
    }

    // Expunging is required if messages were deleted
    if ($delete) {
        $this->expunge();
    }
}

/**
 * Gets the number of messages in a mailbox.
 *
 * @return int Number of messages in the mailbox.
 */
function getNumMessages() {
    return imap_num_msg($this->mbox);
}

/**
 * Deletes a message.
 *
 * @param int $id ID of message to delete.
 * @param bool $expunge Optionally expunge mailbox.
 */
function delete($id, $expunge = false) {
    imap_delete($this->mbox, $id);

    if ($expunge) {
        $this->expunge();
    }
}

/**
 * Expunge a mailbox. Call after deleting messages.
 */
function expunge() {
    imap_expunge($this->mbox);
}
 }

Я очень новичок в работе с почтовыми серверами и тому подобное, поэтому я понятия не имею, как получать электронную почту и запускать с ней скрипт PHP,и т. д.

Я думаю, этот класс поможет мне в этом, поэтому было бы здорово узнать, как его реализовать.

большое спасибо !!

1 Ответ

0 голосов
/ 04 декабря 2011

Чтобы использовать этот класс, вам нужно убедиться, что расширение PHP_IMAP установлено и включено.

Если вы прочитаете комментарии в коде, то обнаружите, что они довольно хорошо документируют использование класса.

Первое, что вам нужно сделать, - создать экземпляр класса, предоставив конструктору почтовый хост, имя пользователя и пароль, например: -

$emailReader = new EmailReader('imap.myhost.com', 'myUserName', 'myPassWord');

После этого нужно просто вызвать нужные вам методы. Например, вы можете узнать, сколько писем в почтовом ящике, например: -

$numMessages = $emailReader->getNumMessages();

Я должен сказать, что этот класс, кажется, не завершен или не очень хорошо написан, и я бы порекомендовал поискать другой класс. Вы можете найти Почтовый пакет Pearl , подходящий для ваших нужд. Это, безусловно, даст вам более полную и лучше написанную базу кода для изучения.

Я должен также предупредить вас, что я не смог заставить этот класс подключиться к двум моим почтовым серверам, которые я пробовал, хотя я не пытался очень долго.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...