It would probably need to be an output renderer override in your theme.
I recently wrote up a post for my own reference about overriding a different core function with links to the relevant Moodle documentation that you might find useful https://www.dgmyspace.dumgal.ac.uk/eportfolio/rumbler/2021/08/14/moodle-theme-building-role-descriptor/
You'll need to figure out where the function is being defined and what the class you need to extend is called. My guess would be somewhere either in /lib or /mod although having done a quick search it might be /message/renderer.php
I hope that at least gives some starting points.
I know it doesn't entirely answer the question!