public function spool($limit = 10)
    {
        $done = 0;
        // find all the spooled emails
        $emailSpools = EmailSpool::model()->findAll(array(
            'condition' => 't.status=:status',
            'params' => array(':status' => 'pending'),
            'order' => 't.priority DESC, t.created ASC',
            'limit' => $limit,
        ));
        foreach ($emailSpools as $emailSpool) {

            // update status to processing
            $emailSpool->status = 'processing';
            $emailSpool->save(false);

            // build the message
            $swiftMessage = $emailSpool->unpack($emailSpool->message);

            // send the email
            $sent = $this->deliver($swiftMessage, $emailSpool->transport);

            // update status and save
            $emailSpool->status = $sent ? 'emailed' : 'error';
            $emailSpool->sent = time();
            $emailSpool->save(false);
            $done++;
        }
        return $done;
    }