public function register(Application $app)
 {
     $app['fetch.initialized'] = false;
     $app['fetch'] = $app->share(function ($app) {
         $options = array_replace(array('host' => 'localhost', 'user' => '', 'password' => '', 'port' => 993), $app['fetch.options']);
         $server = new \Fetch\Server($options['host'], $options['port']);
         if ($options['user']) {
             $server->setAuthentication($options['user'], $options['password']);
         }
         $app['fetch.initialized'] = true;
         return $server;
     });
 }
<?php

require_once 'vendor/autoload.php';
$server = new \Fetch\Server('imap.gmail.com', 993);
$server->setAuthentication('*****@*****.**', 'tochikun');
$messages = $server->getMessages();
/** @var $message \Fetch\Message */
$file_id = 0;
foreach ($messages as $message) {
    $subject = imap_utf8($message->getSubject());
    $str = "Subject: " . $subject . "\nBody: {$message->getMessageBody()}\n";
    echo $str;
    $filename = "app_tmp/" . "{$file_id}" . ".txt";
    file_put_contents($filename, $str);
    $file_id++;
}