0

カスタムフラグなしで特定のフォルダ内のすべてのメールをダウンロードするスクリプトを作成しようとしています。今のところ、フラグを$aNiceFlagと呼びましょう。メールを取得した後、$aNiceFlagでフラグを付けたいと思います。しかし、フラグの問題に取り組む前に、今メールから必要なコンテンツを抽出するのに問題があります。

これは私が必要とする情報です:

  • 送信者(可能であればメールアドレスと名前)
  • 主題
  • レシーバー
  • プレーンテキストの本文(htmlのみが使用可能な場合は、htmlからプレーンテキストに変換してみます)
  • 送信時間

を使用すると、簡単に主題を取得できます$mailObject->subjectZendのドキュメントを見ていますが、少し混乱しています。

これが今の私のコードです。コンテンツをエコーアウトすることは想定されていませんが、テスト中は今のところです。

$this->gOauth = new GoogleOauth();
$this->gOauth->connect_imap();
$storage = new Zend_Mail_Storage_Imap(&$this->gOauth->gImap);
$storage->selectFolder($this->label);
foreach($storage as $mail){
    echo $mail->subject();
    echo strip_tags($mail->getContent());
}

GoogleOAuthを使用してメールにアクセスしています。$this->label取得したいフォルダです。今のところ非常に簡単ですが、複雑にする前に、上記のすべてのデータを配列内の個別のキーに抽出する適切な方法などの基本を理解したいと思います。

4

1 に答える 1

5

件名に使用したのと同じ手法を使用して、送信者、受信者、日付のヘッダーを非常に簡単に取得できますが、実際のプレーンテキストの本文は少し注意が必要です。これが必要な処理を行うサンプルコードです。

    $this->gOauth = new GoogleOauth();
    $this->gOauth->connect_imap();
    $storage = new Zend_Mail_Storage_Imap(&$this->gOauth->gImap);
    $storage->selectFolder($this->label);
    // output first text/plain part
    $foundPart = null;
    foreach($storage as $mail){
        echo '----------------------<br />'."\n";
        echo "From: ".utf8_encode($mail->from)."<br />\n";
        echo "To: ".utf8_encode(htmlentities($mail->to))."<br />\n";
        echo "Time: ".utf8_encode(htmlentities(date("Y-m-d H:s" ,strtotime($mail->date))))."<br />\n";
        echo "Subject: ".utf8_encode($mail->subject)."<br />\n";

        foreach (new RecursiveIteratorIterator($mail) as $part) {
            try {
                if (strtok($part->contentType, ';') == 'text/plain') {
                    $foundPart = $part;
                    break;
                }
            } catch (Zend_Mail_Exception $e) {
                // ignore
            }
        }
        if (!$foundPart) {
            echo "no plain text part found <br /><br /><br /><br />\n\n\n";
        } else {
            echo "plain text part: <br />" .
                    str_replace("\n", "\n<br />", trim(utf8_encode(quoted_printable_decode(strip_tags($foundPart)))))
                ." <br /><br /><br /><br />\n\n\n";
        }
    }
于 2011-05-19T14:49:31.517 に答える