Today we are going back to basics of PHP with a quick tutorial on how to check email address using network functions of PHP.Network functions are the core part of PHP and most of are not using this functions so most of from us are not aware of those function which are very useful.
Here,I am going to show one function which check email address valid or not using a network function.This something interesting right?
So, let’s understand the function and how we can use network functions.First of all ,it checks if the email address format is correct, then looks up and prints the mx records.
For example, if user enter testttttt.com, and the domain does not exist, getmxrr() returns falses and the returned mxhosts array is empty
In the function,I have used checkdnsrr function which Check DNS records corresponding to a provided Internet host name or IP address.
Functions to check Email Address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | function CheckMail($Email,$Debug=false) { $Return = false; if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) { echo "Email address is not valid"; $Return ='false'; return $Return; } list ( $Username, $Domain ) = explode ("@",$Email); if ( checkdnsrr ( $Domain, "MX" ) ) { if($Debug) echo "Confirmation : MX record about {$Domain} exists.<br>"; if ( getmxrr ($Domain, $MXHost)) { for ( $i = 0,$j = 1; $i < count ( $MXHost ); $i++,$j++ ) { // check for gmail email address { echo "Email address is of Gmail"; $Return = 'true'; return $Return; } else{ echo "Email address is valid"; $Return = 'true'; return $Return; } if($Debug) { echo "Result($j) - $MXHost[$i]<BR>"; } } if($Return == 'false' && count ( $MXHost ) > 0) $Return = true; } $ConnectAddress = $MXHost[0]; } else { echo "Email address is not valid"; $Return=false; //if ($Debug) echo "Can not connect E-Mail server ({$ConnectAddress}).<br>"; return $Return; } } |
You just need to call the function and you can do changes in function as per your requirement.
A big thank you for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.