File: 0.04.05a/server/base/tests/smtp.inc.php (View as HTML)

  1: <?php // smtp.inc.php -- SMTP test
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: function smtp_test_connect($hostname,$port=25,$timeout=-1,$debug=false)
 24: {
 25: global $NATS;
 26: if ($timeout>0) $timeout=$timeout; // use specific for test if set
 27: else
 28: 	{
 29: 	// otherwise use system if available
 30: 	if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.smtp.timeout",0);
 31: 	if ($timeout<=0) $timeout=20; // unset specifically or in environment
 32: 	}
 33: 	
 34: if ($port=="") $port=25;
 35: $errno=0;
 36: $errstr=0;
 37: $fp=@fsockopen($hostname,$port,$errno,$errstr,$timeout);
 38: if ($errno!=0) return 0;
 39: if ($fp<=0) return 0;
 40: stream_set_timeout($fp,$timeout);
 41: $header=@fgets($fp,128);
 42: if ($debug) echo $header."\n";
 43: if (substr($header,0,3)!="220") return 0;
 44: if (@fputs($fp,"HELO freenats\n")===false) return 0;
 45: $body=@fgets($fp,128);
 46: if ($debug) echo $body."\n";
 47: if ($body=="") return 0;
 48: if (@fputs($fp,"QUIT\n")===false) return 0;
 49: @fclose($fp);
 50: return 1;
 51: }
 52: 
 53: function smtp_test_time($hostname,$port=25,$timeout=-1,$debug=false)
 54: {
 55: $timer=new TFNTimer();
 56: $timer->Start();
 57: $res=smtp_test_connect($hostname,$port,$timeout,$debug);
 58: $time=$timer->Stop();
 59: if ($res<=0) return $res; // connect failed
 60: $time=round($time,4);
 61: if ($time==0) $time=0.0001;
 62: return $time;
 63: }
 64: 
 65: ?>