phpanon Posted March 23, 2008 Share Posted March 23, 2008 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(); Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/ Share on other sites More sharing options...
MadTechie Posted March 23, 2008 Share Posted March 23, 2008 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-499062 Share on other sites More sharing options...
phpanon Posted March 24, 2008 Author Share Posted March 24, 2008 Hi, thanks for taking a loook. Is it possible for u to explain what this bit of code does?! Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-499082 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-499085 Share on other sites More sharing options...
phpanon Posted March 25, 2008 Author Share Posted March 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500012 Share on other sites More sharing options...
MadTechie Posted March 25, 2008 Share Posted March 25, 2008 Other than the warning did it handle the failed connection okay ? also to hide the "warning" append a @ to it $fp = @fsockopen($mHost, $mPort, $errno, $errstr, 30); //Connects to the mail server (wait a max of 30 seconds) Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500017 Share on other sites More sharing options...
phpanon Posted March 25, 2008 Author Share Posted March 25, 2008 Now im getting an "HTTP 500 Internal Server Error" and it doesn't seem to be writing the query to the database even if it doesnt connect to the server. Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500028 Share on other sites More sharing options...
phpanon Posted March 25, 2008 Author Share Posted March 25, 2008 can anyone help with this?? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500354 Share on other sites More sharing options...
MadTechie Posted March 25, 2008 Share Posted March 25, 2008 Adding the @ caused a error 500 ? remove the @ and post all the errors.. as a surpressed warning shouldn't cause an error 500! Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500561 Share on other sites More sharing options...
phpanon Posted March 25, 2008 Author Share Posted March 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500616 Share on other sites More sharing options...
MadTechie Posted March 25, 2008 Share Posted March 25, 2008 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! ?> Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500633 Share on other sites More sharing options...
phpanon Posted March 25, 2008 Author Share Posted March 25, 2008 Thanks, that works! Quote Link to comment https://forums.phpfreaks.com/topic/97511-crash-if-email-server-not-connected/#findComment-500699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.