You should not use the above code.
Instead, look for an "A" or "MX" for the domain. If either exists (i.e. returns a valid IP), then the domain exists for mail (although it may not have a working SMTP server or accept mail).
You should not assume that because the mail server doesn't have a listening port 25 at some particular instant in time that it never will.
You should also not check port 80 on a mail server, because mail servers do not need to (and typically don't) have HTTP servers on them.
The above example is rubbish because it's looking for specific host names in the domain, like mx and smtp, pop etc.
There is no guarantee that a given domain will have such hosts - it could still accept mail.
Anyway, who's to say that mydomain.com doesn't have its mail served by blargh.wibble.org.uk ?
Just do an MX and A lookup (in that order).
Of course, if you get a temporary error like server failure, you can't take that to mean the domain doesn't exist.
Mark