I’m using PHP’s SwiftMailer library to bulk send emails (following CANSPAM and RFCs).
Everything works fine until I run across a domain that does not have a corresponding MX entry in the DNS. At that point, my script just hangs — I’m assuming it’s because sendmail hasn’t returned. This can last just a few minutes, or it can last many hours.
I have confirmed that the addresses my script (slash sendmail) hang on are those without a DNS MX entry. Everything else sends fine.
Is there a way I can check if the DNS MX exists inside of PHP before I attempt to send the message? Is this scalable (i.e., will that extra lookup on the DNS really slow me down, or will it be fine b/c then the MX is cached locally for sendmail to use in delivering the message)?
Thanks for your help
— UPDATE —
I tried the solutions below, which helped me narrow down what the problem is. It seems it’s not a question of whether or not the MX entry exists. The problem is that the DNS lookup is taking forever to return. Is there a simple way I can set the timeout, so if DNS doesn’t return in < 5s, I can move on?
You can also try this to prevent blocking (there are no options for adjusting
getmxrrtimeout):