public function generate($userid, $userkey = null) {
        global $CFG;

        // Ensure that Inbound Message is enabled and that there is enough information to proceed.
        if (!manager::is_enabled()) {
            return null;
        }

        if ($userkey == null) {
            $userkey = get_user_key('messageinbound_handler', $userid);
        }

        // Ensure that the minimum requirements are in place.
        if (!isset($this->handler) || !$this->handler) {
            throw new \coding_exception('Inbound Message handler not specified.');
        }

        // Ensure that the requested handler is actually enabled.
        if (!$this->handler->enabled) {
            return null;
        }

        if (!isset($this->datavalue)) {
            throw new \coding_exception('Inbound Message data item has not been specified.');
        }

        $data = array(
            self::pack_int($this->handler->id),
            self::pack_int($userid),
            self::pack_int($this->datavalue),
            pack('H*', substr(md5($this->fetch_data_key() . $userkey), 0, self::HASHSIZE)),
        );
        $subaddress = base64_encode(implode($data));

        return $CFG->messageinbound_mailbox . '+' . $subaddress . '@' . $CFG->messageinbound_domain;
    }