Jump to content

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      = 'alexbowell@googlemail.com';
		$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: roomBookings@rias.co.uk' . "\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      = 'alexbowell@googlemail.com';
			$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: roomBookings@rias.co.uk' . "\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!
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.