public function gen_message_id($sender = null)
    {
        $local_part  = md5(uniqid('rcube'.mt_rand(), true));
        $domain_part = '';

        if ($sender && preg_match('/@([^\s]+\.[a-z0-9-]+)/', $sender, $m)) {
            $domain_part = $m[1];
        }
        else {
            $domain_part = $this->user->get_username('domain');
        }

        // Try to find FQDN, some spamfilters doesn't like 'localhost' (#1486924)
        if (!preg_match('/\.[a-z0-9-]+$/i', $domain_part)) {
            foreach (array($_SERVER['HTTP_HOST'], $_SERVER['SERVER_NAME']) as $host) {
                $host = preg_replace('/:[0-9]+$/', '', $host);
                if ($host && preg_match('/\.[a-z]+$/i', $host)) {
                    $domain_part = $host;
                    break;
                }
            }
        }

        return sprintf('<%s@%s>', $local_part, $domain_part);
    }