protected function getEmail(): AbstractMailAdapter
    {
        if (isset($this->config[self::FN_CONFIG_KEY])) {
            $mail = call_user_func($this->config[self::FN_CONFIG_KEY], $this->messages);

            if (!($mail instanceof AbstractMailAdapter)) {
                throw new Exception('Function defined in mail config under ' . self::FN_CONFIG_KEY . ' must return instance of \Koldy\Mail\Adapter\AbstractMailAdapter; ' . gettype($mail) . ' given');
            }

            return $mail;
        }

        if (count($this->messages) == 0) {
            throw new Exception('Can not prepare e-mail instance when there\'s no log messages added from before; try not to call getEmail() if messages array is empty');
        }

        $mail = Mail::create($this->config['adapter']);
        $allMessages = [];
        $firstMessage = $this->messages[0];

        foreach ($this->messages as $msg) {
            $allMessages[] = $msg->getDefaultLine();
        }

        $body = implode("\n", $allMessages);

        $level = strtoupper($firstMessage->getLevel());
        $subject = "[{$level}] {$firstMessage->getMessage()}";

        $body .= "\n\n----------\n";
        $body .= Server::signature();
        $subject = Util::truncate($subject, 140);
        $domain = Application::getDomain();

        $mail->from('alert@' . $domain, $domain)->subject($subject)->body($body);

        $to = $this->config['to'];
        if (!is_array($this->config['to']) && strpos($this->config['to'], ',') !== false) {
            $to = explode(',', $this->config['to']);
        }

        if (is_array($to)) {
            foreach ($to as $toEmail) {
                $mail->to(trim($toEmail));
            }
        } else if (is_string($to)) {
            $mail->to(trim($to));
        } else {
            throw new Exception('Unable to prepare log via email, expected array or string for \'to\', got ' . gettype($to));
        }

        return $mail;
    }