Your problem is here:
$msg = ereg_replace ("\r\n", "<br>", $msg);
When trying to work with escape characters you'll need to add more escapes. I know it sounds wierd, but the above code isn't searching inside your $msg string for the \r\n that you are sending it, instead it's searching for the actual carrige return linefeed characters, and it won't be able to find them. You'll proabably have to do this:
$msg = ereg_replace ("\\r\\n", "<br>", $msg);
Escaping your escape characters will send the \r\n correctly to the ereg_replace function and hopefully work to do the replace. However depending on how the ereg_replace function was coded you may need to add more escapes to make sure that by the time the code gets to the actual replacement engine it's getting \r\n. In some things I've done I've ended up with things like \\r\\n.