3v4l.org

run code in 300+ PHP versions simultaneously
<?php // the email to validate $email = 'user@example.com'; // an optional sender $sender = 'user@mydomain.com'; // instantiate the class $SMTP_Validator = new SMTP_validateEmail(); // turn on debugging if you want to view the SMTP transaction $SMTP_Validator->debug = true; // do the validation $results = $SMTP_Validator->validate(array($email), $sender); // view results echo $email.' is '.($results[$email] ? 'valid' : 'invalid')."\n"; // send email? if ($results[$email]) { //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\r\n"); //send email echo 'VALID EMAIL!'; } else { echo 'The email addresses you entered is not valid'; } /** * Validate Email Addresses Via SMTP * This queries the SMTP server to see if the email address is accepted. * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact * @author gabe@fijiwebdesign.com * @contributers adnan@barakatdesigns.net * @version 0.1a */ class SMTP_validateEmail { /** * PHP Socket resource to remote MTA * @var resource $sock */ var $sock; /** * Current User being validated */ var $user; /** * Current domain where user is being validated */ var $domain; /** * List of domains to validate users on */ var $domains; /** * SMTP Port */ var $port = 25; /** * Maximum Connection Time to wait for connection establishment per MTA */ var $max_conn_time = 30; /** * Maximum time to read from socket before giving up */ var $max_read_time = 5; /** * username of sender */ var $from_user = 'user'; /** * Host Name of sender */ var $from_domain = 'localhost'; /** * Nameservers to use when make DNS query for MX entries * @var Array $nameservers */ var $nameservers = array( '192.168.0.1' ); var $debug = false; /** * Initializes the Class * @return SMTP_validateEmail Instance * @param $email Array[optional] List of Emails to Validate * @param $sender String[optional] Email of validator */ function SMTP_validateEmail($emails = false, $sender = false) { if ($emails) { $this->setEmails($emails); } if ($sender) { $this->setSenderEmail($sender); } } function _parseEmail($email) { $parts = explode('@', $email); $domain = array_pop($parts); $user= implode('@', $parts); return array($user, $domain); } /** * Set the Emails to validate * @param $emails Array List of Emails */ function setEmails($emails) { foreach($emails as $email) { list($user, $domain) = $this->_parseEmail($email); if (!isset($this->domains[$domain])) { $this->domains[$domain] = array(); } $this->domains[$domain][] = $user; } } /** * Set the Email of the sender/validator * @param $email String */ function setSenderEmail($email) { $parts = $this->_parseEmail($email); $this->from_user = $parts[0]; $this->from_domain = $parts[1]; } /** * Validate Email Addresses * @param String $emails Emails to validate (recipient emails) * @param String $sender Sender's Email * @return Array Associative List of Emails and their validation results */ function validate($emails = false, $sender = false) { $results = array(); if ($emails) { $this->setEmails($emails); } if ($sender) { $this->setSenderEmail($sender); } // query the MTAs on each Domain foreach($this->domains as $domain=>$users) { $mxs = array(); // current domain being queried $this->domain = $domain; // retrieve SMTP Server via MX query on domain list($hosts, $mxweights) = $this->queryMX($domain); for($n=0; $n < count($hosts); $n++){ $mxs[$hosts[$n]] = $mxweights[$n]; } asort($mxs); // last fallback is the original domain $mxs[$this->domain] = 0; $this->debug(print_r($mxs, 1)); $timeout = $this->max_conn_time; // try each host while(list($host) = each($mxs)) { // connect to SMTP server $this->debug("try $host:$this->port\n"); if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) { stream_set_timeout($this->sock, $this->max_read_time); break; } } // did we get a TCP socket if ($this->sock) { $reply = fread($this->sock, 2082); $this->debug("<<<\n$reply"); preg_match('/^([0-9]{3}) /ims', $reply, $matches); $code = isset($matches[1]) ? $matches[1] : ''; if($code != '220') { // MTA gave an error... foreach($users as $user) { $results[$user.'@'.$domain] = false; } continue; } // say helo $this->send("HELO ".$this->from_domain); // tell of sender $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">"); // ask for each recepient on this domain foreach($users as $user) { // ask of recepient $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">"); // get code and msg from response preg_match('/^([0-9]{3}) /ims', $reply, $matches); $code = isset($matches[1]) ? $matches[1] : ''; if ($code == '250') { // you received 250 so the email address was accepted $results[$user.'@'.$domain] = true; } elseif ($code == '451' || $code == '452') { // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok $results[$user.'@'.$domain] = true; } else { $results[$user.'@'.$domain] = false; } } // reset before quit $this->send("RSET"); // quit $this->send("quit"); // close socket fclose($this->sock); } else { $this->debug('Error: Could not connect to a valid mail server for this email address: ' . $user.'@'.$domain); } } return $results; } function send($msg) { fwrite($this->sock, $msg."\r\n"); $reply = fread($this->sock, 2082); $this->debug(">>>\n$msg\n"); $this->debug("<<<\n$reply"); return $reply; } /** * Query DNS server for MX entriesg * @return */ function queryMX($domain) { $hosts = array(); $mxweights = array(); if (function_exists('getmxrr')) { getmxrr($domain, $hosts, $mxweights); } else { // windows, we need Net_DNS require_once 'Net/DNS.php'; $resolver = new Net_DNS_Resolver(); $resolver->debug = $this->debug; // nameservers to query $resolver->nameservers = $this->nameservers; $resp = $resolver->query($domain, 'MX'); if ($resp) { foreach($resp->answer as $answer) { $hosts[] = $answer->exchange; $mxweights[] = $answer->preference; } } } return array($hosts, $mxweights); } /** * Simple function to replicate PHP 5 behaviour. http://php.net/microtime */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } function debug($str) { if ($this->debug) { echo '<pre>'.htmlentities($str).'</pre>'; } } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0090.00918.77
8.3.50.0130.00721.32
8.3.40.0080.00819.34
8.3.30.0040.01119.20
8.3.20.0090.00020.72
8.3.10.0040.00423.90
8.3.00.0050.00324.09
8.2.180.0110.00718.67
8.2.170.0150.00022.96
8.2.160.0060.00922.45
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0000.00826.16
8.2.120.0090.00018.00
8.2.110.0040.00821.25
8.2.100.0060.00618.13
8.2.90.0000.00819.44
8.2.80.0060.00317.97
8.2.70.0040.00418.13
8.2.60.0000.00818.30
8.2.50.0030.00618.07
8.2.40.0030.00621.26
8.2.30.0040.00420.19
8.2.20.0050.00318.13
8.2.10.0000.00818.48
8.2.00.0030.00618.35
8.1.280.0150.00025.92
8.1.270.0030.00620.57
8.1.260.0060.00326.35
8.1.250.0040.00428.09
8.1.240.0060.00323.98
8.1.230.0040.00717.91
8.1.220.0060.00318.04
8.1.210.0050.00318.77
8.1.200.0100.00017.72
8.1.190.0000.00917.61
8.1.180.0040.00418.10
8.1.170.0090.00018.91
8.1.160.0080.00022.29
8.1.150.0040.00419.14
8.1.140.0040.00417.74
8.1.130.0000.00717.91
8.1.120.0030.00617.61
8.1.110.0000.00817.63
8.1.100.0070.00017.79
8.1.90.0080.00017.55
8.1.80.0090.00017.61
8.1.70.0040.00417.65
8.1.60.0090.00017.89
8.1.50.0080.00017.80
8.1.40.0000.00917.61
8.1.30.0000.00817.78
8.1.20.0000.00817.83
8.1.10.0030.00617.67
8.1.00.0040.00417.71
8.0.300.0030.00518.77
8.0.290.0040.00417.13
8.0.280.0050.00318.66
8.0.270.0030.00317.48
8.0.260.0000.00717.53
8.0.250.0090.00017.16
8.0.240.0040.00417.15
8.0.230.0080.00017.16
8.0.220.0000.00717.10
8.0.210.0000.00717.03
8.0.200.0030.00717.12
8.0.190.0000.00917.16
8.0.180.0000.00817.29
8.0.170.0070.00217.16
8.0.160.0030.00517.30
8.0.150.0020.00517.11
8.0.140.0030.00617.25
8.0.130.0070.00013.73
8.0.120.0030.00517.29
8.0.110.0060.00317.26
8.0.100.0030.00517.10
8.0.90.0040.00417.05
8.0.80.0060.00917.10
8.0.70.0080.00017.00
8.0.60.0040.00417.06
8.0.50.0030.00517.20
8.0.30.0120.01317.39
8.0.20.0100.01117.40
8.0.10.0050.00317.32
8.0.00.0100.00817.09
7.4.330.0000.00515.45
7.4.320.0070.00016.77
7.4.300.0000.00716.76
7.4.290.0040.00416.80
7.4.280.0050.00316.77
7.4.270.0000.00716.78
7.4.260.0040.00416.79
7.4.250.0040.00416.90
7.4.240.0020.00716.91
7.4.230.0080.00016.89
7.4.220.0170.00016.96
7.4.210.0040.01216.94
7.4.200.0040.00416.91
7.4.160.0060.01016.88
7.4.150.0060.01317.40
7.4.140.0130.01117.86
7.4.130.0190.00416.88
7.4.120.0150.00616.94
7.4.110.0200.00716.91
7.4.100.0150.00916.86
7.4.90.0110.00716.91
7.4.80.0090.00919.39
7.4.70.0120.00916.86
7.4.60.0120.00916.79
7.4.50.0050.00016.81
7.4.40.0140.00417.10
7.4.30.0070.01416.85
7.4.00.0070.00715.27
7.3.330.0080.00013.68
7.3.320.0000.00613.77
7.3.310.0040.00416.66
7.3.300.0030.00516.78
7.3.290.0050.00216.77
7.3.280.0050.01616.80
7.3.270.0070.01117.40
7.3.260.0190.00416.82
7.3.250.0120.00916.88
7.3.240.0090.00916.93
7.3.230.0100.01016.91
7.3.210.0060.01416.85
7.3.200.0140.01116.84
7.3.190.0070.01316.90
7.3.180.0060.01217.00
7.3.170.0040.01816.94
7.3.160.0120.00916.68
7.2.330.0050.01416.73
7.2.320.0150.00917.05
7.2.310.0100.00717.03
7.2.300.0120.00617.16
7.2.290.0170.00517.02
7.2.60.0080.00617.19
7.2.00.0060.00919.71
7.1.200.0080.00416.12
7.1.100.0130.00518.52
7.1.70.0040.00417.63
7.1.60.0160.00919.12
7.1.50.0120.00817.16
7.1.00.0070.07322.64
7.0.200.0040.00916.82
7.0.100.0070.07720.25
7.0.90.0070.07720.43
7.0.80.0100.05320.33
7.0.70.0130.07720.30
7.0.60.0370.08020.21
7.0.50.0030.08720.59
7.0.40.0070.08720.41
7.0.30.0100.08020.54
7.0.20.0100.08320.59
7.0.10.0130.08720.55
7.0.00.0070.08020.58
5.6.280.0030.06721.47
5.6.250.0030.08021.09
5.6.240.0070.07321.00
5.6.230.0100.07321.00
5.6.220.0130.07720.90
5.6.210.0030.08721.07
5.6.200.0170.07721.57
5.6.190.0100.08321.42
5.6.180.0100.08321.57
5.6.170.0100.08021.57
5.6.160.0170.07721.60
5.6.150.0070.08021.45
5.6.140.0070.08721.64
5.6.130.0100.07721.43
5.6.120.0070.05321.31
5.6.110.0070.08321.28
5.6.100.0070.05721.57
5.6.90.0070.08321.34
5.6.80.0000.06320.94
5.6.70.0170.07320.88
5.6.60.0070.08020.82
5.6.50.0170.07320.68
5.6.40.0130.06720.76
5.6.30.0200.07020.84
5.6.20.0070.05320.66
5.6.10.0100.07320.84
5.6.00.0100.08020.84
5.5.380.0130.07320.75
5.5.370.0100.08020.65
5.5.360.0070.07720.89
5.5.350.0170.07020.83
5.5.340.0030.05721.28
5.5.330.0130.07321.32
5.5.320.0170.07321.35
5.5.310.0070.08321.09
5.5.300.0070.08321.24
5.5.290.0030.08321.15
5.5.280.0100.07721.24
5.5.270.0100.07721.34
5.5.260.0070.08321.36
5.5.250.0030.08321.14
5.5.240.0070.08320.78
5.5.230.0030.09020.49
5.5.220.0170.07020.48
5.5.210.0130.07720.67
5.5.200.0070.08020.52
5.5.190.0030.08320.71
5.5.180.0030.08320.59
5.5.160.0100.07720.43
5.5.150.0130.08020.63
5.5.140.0100.07320.56
5.5.130.0070.07720.59
5.5.120.0070.07320.50
5.5.110.0130.07720.74
5.5.100.0070.04720.62
5.5.90.0030.05020.42
5.5.80.0070.07720.61
5.5.70.0130.05020.63
5.5.60.0130.06720.38
5.5.50.0100.07720.58
5.5.40.0070.08020.54
5.5.30.0000.06720.55
5.5.20.0270.06320.61
5.5.10.0070.07720.47
5.5.00.0170.07020.32
5.4.450.0100.08319.77
5.4.440.0100.07719.82
5.4.430.0070.05019.80
5.4.420.0070.07019.95
5.4.410.0170.06719.68
5.4.400.0100.07019.19
5.4.390.0070.07719.63
5.4.380.0070.07019.32
5.4.370.0000.08719.40
5.4.360.0070.07319.45
5.4.350.0200.04719.32
5.4.340.0070.08019.59
5.4.320.0100.07719.48
5.4.310.0170.07719.46
5.4.300.0200.06719.32
5.4.290.0170.07319.43
5.4.280.0130.06319.37
5.4.270.0100.07719.45
5.4.260.0130.07319.59
5.4.250.0070.08019.49
5.4.240.0130.05719.49
5.4.230.0130.07019.46
5.4.220.0130.05019.36
5.4.210.0130.07019.29
5.4.200.0100.07019.29
5.4.190.0030.07719.47
5.4.180.0070.08319.45
5.4.170.0130.07019.48
5.4.160.0170.07019.37
5.4.150.0170.06319.48
5.4.140.0030.08016.81
5.4.130.0100.07016.73
5.4.120.0070.07716.73
5.4.110.0030.05016.82
5.4.100.0170.05016.77
5.4.90.0030.07716.68
5.4.80.0000.08016.84
5.4.70.0070.08016.80
5.4.60.0100.06716.83
5.4.50.0070.05716.91
5.4.40.0030.08016.88
5.4.30.0100.07016.86
5.4.20.0070.07716.78
5.4.10.0070.06316.88
5.4.00.0100.04716.02

preferences:
62.93 ms | 401 KiB | 5 Q