} catch (ParameterException $e) {
}
// Set tags
try {
    $data->tags = explode(" ", param_variable('tags'));
} catch (ParameterException $e) {
}
if (get_config('licensemetadata')) {
    // Set licensing information
    try {
        $license = license_coalesce(null, param_variable('license'), param_variable('license_other', null));
        $licensor = param_variable('licensor');
        $licensorurl = param_variable('licensorurl');
    } catch (ParameterException $e) {
    }
}
try {
    $newid = ArtefactTypeFile::save_uploaded_file('userfile', $data);
} catch (QuotaExceededException $e) {
    JSONResponse('fail', 'Quota exceeded');
} catch (UploadException $e) {
    JSONResponse('fail', 'Failed to save file');
}
// Here we need to create a new hash - update our own store of it and return it too the handset
JSONResponse("success", $USER->refresh_mobileuploadtoken($token));
function JSONResponse($key, $value)
{
    header('Content-Type: application/json');
    echo json_encode(array($key => $value));
    exit;
}
    // up other information along with the form
    $template_data = $json["formData"];
} else {
    // this is the entire form, $_REQUEST["name"] will be the name, if you need
    // more stuff here, add a hidden input.
    $template_data = $_REQUEST;
}
try {
    // this is the main function, an exception will be thrown if this fails.
    send_template_email(array("to" => "*****@*****.**", "cc" => array("*****@*****.**", "*****@*****.**"), "bcc" => null, "subject" => "This is the subject", "bodyTemplate" => "email_template", "data" => $template_data));
    // handle the success
    if ($is_ajax) {
        JSONResponse(array("message" => "Email sent successfully."));
    } else {
        //redirect with no ajax
        header("Location: test.html?success=true");
    }
} catch (PEARErrorException $e) {
    // This exception class is a wrapper for receiving the Pear error
    // object when the email could not be sent.
    $error = $e->error;
    // if the error has a newline, it will break, seems google will return with newlines for invalid user/pass
    $message = str_replace("\n", " ", $error->message);
    if ($is_ajax) {
        JSONResponse(array("error" => $message), false);
    } else {
        // THIS IS A POST SUBMIT ERROR, you will most likely want to redirect, you may
        // want to redirect to a PHP page and use $_SESSION to transfer the error string
        header("Location: test.html?error=" . $message);
    }
}