When users enter an email address in a form it is quite useful to be able to validate that email, both for security and accuracy.
One of the built in functions of PHP is to validate an email address.
Line 5: we run the filter_var function with an option to validate the email address.
Lines 7 to 11: if the email is not valid then we can print out a message. At this point we would probably wish to redirect the user back to the form.
However, what if the user entered an email which was in the correct format but did not belong to a valid domain. We can both validate and check for domain validity as follows:
Line 5: we call the checkEmail function passing in the argument of the email address
Lines 11 to 24: the checkEmail function
Line 13: the first thing that is done is the validate the format of the email address. If it is not valid then a false value is returned.
Line 17: the emall address is separated at the @ symbol so that the domain name can be retrieved. By using the end() function we can store the domain name in the variable $host.
Line 19: we check the dns records for the domain. Both the MX and A records are checked. If they are not valid then a false value is returned.
Line 23: if the domain name and email format are both valid then a true value is returned.