Jump to content

crash if email server not connected


phpanon

Recommended Posts

Hello there,

 

Is there any code that i can add to my script that will catch the error if the email server is not connected and just run the code without sending the email without crashing.

 

$to      = '[email protected]';
		$subject = "Room ".$roomID." Successfully Booked";
		$message = "You have successfully booked a Meeting Room. \n \n Meeting Room : ".$roomID." \n Date: ".$bkgDate." \n Time Slot: ".$bkgTime.". \n \n This is a confirmation email. Do not reply. \n \n \n \n RIAS Plc \n Insurance for the over 50's.";
		$headers = 'From: [email protected]' . "\r\n" .
    					'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);

		$query2 = "insert into booking values ('','".$roomID."','".$empID."','".$bkgDate."','".$bkgTime."')";
		$result2 = mysql_query($query2, $connection) or die ("Unable to perform query<br>$query2");

		$message4 = "Meeting Room Booked ";
		header("Location: MeetingMain.php?message4=$message4");
		exit();

Link to comment
https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/
Share on other sites

mail($to, $subject, $message, $headers);

will return false if it fails

 

maybe this!! before mailing!

<?php
$mHost = ini_get('SMTP');
$mPort = ini_get('smtp_port');
$fp = fsockopen($mHost, $mPort, $errno, $errstr, 30);
if (!$fp)
{
die("Mail Server down");
}
?>

sure

<?php
$mHost = ini_get('SMTP'); //Gets SMTP (out going mail servers domain/ip) from the php.ini file
$mPort = ini_get('smtp_port'); //Gets SMTP port (out going mail servers port) from the php.ini file probably (port 110)
$fp = fsockopen($mHost, $mPort, $errno, $errstr, 30); //Connects to the mail server (wait a max of 30 seconds)
if (!$fp) //if their was a connection then the server and port are open so should be okay 
{
die("Mail Server down");
}
?>

 

of course the mail server could be open on that server and the port be okay but another problems could mess things up, but then your need to add more tests,

something like

<?php
$data = "";
while (!feof($fp)) {
        $data .= fgets($fp, 128);
    }
//check $data for messages etc
?>

Hi thanks,

 

I have implemented that code as such...

else
	{

		$mHost = ini_get('SMTP'); //Gets SMTP (out going mail servers domain/ip) from the php.ini file
		$mPort = ini_get('smtp_port'); //Gets SMTP port (out going mail servers port) from the php.ini file probably (port 110)
		$fp = fsockopen($mHost, $mPort, $errno, $errstr, 30); //Connects to the mail server (wait a max of 30 seconds)
		if (!$fp) //if their was a connection then the server and port are open so should be okay 
		{
			$query2 = "insert into booking values ('','".$roomID."','".$empID."','".$bkgDate."','".$bkgTime."')";
			$result2 = mysql_query($query2, $connection) or die ("Unable to perform query<br>$query2");

			$message4 = "Meeting Room Booked ";
			header("Location: MeetingMain.php?message4=$message4");
			exit();
		}
		else
		{
			$to      = '[email protected]';
			$subject = "Room ".$roomID." Successfully Booked";
			$message = "You have successfully booked a Meeting Room. \n \n Meeting Room : ".$roomID." \n Date: ".$bkgDate." \n Time Slot: ".$bkgTime.". \n \n This is a confirmation email. Do not reply. \n \n \n \n RIAS Plc \n Insurance for the over 50's.";
			$headers = 'From: [email protected]' . "\r\n" .
    					'X-Mailer: PHP/' . phpversion();

			mail($to, $subject, $message, $headers);

			$query2 = "insert into booking values ('','".$roomID."','".$empID."','".$bkgDate."','".$bkgTime."')";
			$result2 = mysql_query($query2, $connection) or die ("Unable to perform query<br>$query2");

			$message4 = "Meeting Room Booked ";
			header("Location: MeetingMain.php?message4=$message4");
			exit();
		}
	}

 

But now I am getting this error

 

Warning: fsockopen() [function.fsockopen]: unable to connect to localhost:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\wamp\www\Build\MeetingQuery.php on line 41

 

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\Build\MeetingQuery.php on line 41

 

I get the message i posted earlier if i remove the @...

 

Warning: fsockopen() [function.fsockopen]: unable to connect to localhost:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\wamp\www\Build\MeetingQuery.php on line 41

 

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\Build\MeetingQuery.php on line 41

Ahh your time limit is also 30 seconds

try this

<?php
set_time_limit(30); //reset timeout. 
//and reduce the timout on the socket
$fp = @fsockopen($mHost, $mPort, $errno, $errstr, 20); //Connects to the mail server (wait a max of 20 seconds)
set_time_limit(30); //reset timeout. again!
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.