Connecting grounds between two pieces of independently mains-powered equipment is frequently dangerous. Look up the term "ground loops".
Optoisolators are much cheaper and more reliable than relays. Basic hookup: