Very difficult to tell what could be the problem with such limited info.
Do the servers have the same PHP version? What’s the actual PHP code of the script? Did you check the server logs to see if it’s an issue with PHP itself or maybe the SMTP server? What does “doesn’t work” actually mean — mail is sent but doesn’t arrive, mail is not even sent, etc.