There are a few issues with your code. Category codes are fixed when TeX reads an argument, and so reading anything with altered codes is tricky. That applies both to the use of
\def\mailto and the way you have grabbed the argument of
\mailto before you alter category codes. That might lead you to something like
Notice that I've split the group used for
\mailto, so that it starts before I grab the argument, and ends after I've grabbed it.
However, this won't work for a URL as the
url package does a verbatim read here. In any case, I would avoid active characters (not really needed), and instead to a search-and-replace. I would do this with something like
As I said,
\url deliberately works to print verbatim, so to get the math mode substitution you want will be a little more work!