New Coder Posted April 26, 2010 Share Posted April 26, 2010 Hello all, I have written the following page so that when a member calls to say they will not be in for training it will email all of their trainers (1 or more) and send them a confirmation email to say that this has been logged. If the query finds 3 or more trainers for a member it takes ages to send all the emails. Sometimes the logger is getting impatient and clicking save twice therefore saving a record twice. Or they are complaining they have more to log and are having to wait till they can. It might be that it's just email that is being slow and not the scripts fault. However doubtful and in any case I'm sure it can be improved. <?php include("../php_include/auth_coookies.php"); include("../php_include/connection.php"); include("../php_include/functions.php"); $absent_date = $_POST['absent_date']; $call_time = $_POST['hour'] .":". $_POST['mins']; $reason = (! get_magic_quotes_gpc ()) ? addslashes ($_POST['reason']) : $_POST['reason']; $reason = fix_quotes($reason); $log_date = date ("d/m/Y"); $log_time = date("G:i"); $member_id = str_replace("\'","'",$_POST['member_id']); $member_name = str_replace("\'","'",$_POST['member_name']); $sent = $_POST['sent']; if( !$sent ) { echo( "Error - Data not sent from main form." ); } else { $sql = "SELECT * from members_trainers where member_id = \"$member_id\" "; $rs = mssql_query( $sql, $conn ) or die( "Could not execute Save Absence Log Query" ); $num = mssql_num_rows($rs); if ( $num !=0 ) { if (mssql_num_rows($rs)) { $re = "An absence has been logged for $member_name ($member_id)"; $msg = "A call has been logged for $member_name ($member_id) \n\nReason: $reason \n\nDate of Absence: $absent_date \n\nTime of Call: $call_time \n\nLog Date: $log_date \n\nLog Time: $log_time \n\nLogger: $login_id \n\nThank You"; $headers = "From: No-Reply@our_domain.com \r\n"; while ($row = mssql_fetch_array($rs)) { $staffname = $row["staffname"]; $to = "$staffname@our_domain.com"; mail( $to, $re, $msg, $headers ); } } $sql2 = "insert into att_log (member_id, reason, absent_date, call_time, log_date, log_time, logger) values (\"$member_id\", \"$reason\", \"$absent_date\", \"$call_time\", \"$log_date\", \"$log_time\", \"$login_id\") "; $rs2 = mssql_query( $sql2, $conn ) or die( "Could not execute Save Absence Log Query 2"); $to = "member_id@our_domain.com"; $re = "Absence logged for you - $memebr_name ($member_id)"; $msg = "An absence has been logged for you - $member_name ($member_id) \n\nReason: $reason \n\nDate of Absence: $absent_date \n\nTime of Call: $call_time \n\nLog Date: $log_date \n\nLog Time: $log_time \n\nLogger: $login_id \n\nThank You"; $headers = "From: No-Reply@our_domain.com \r\n"; if( mail( $to, $re, $msg, $headers ) ) { echo("Thank you, An email has been sent to the Members Trainer as notification of this absence.<br><br>An email confirming this absence has been sent to the members email account.<br><form action=\"att_log_2.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } else { echo("Error.....Email not sent to Member. <br>This may be because the Member doesn't have an email account.<br>Please go back and check the record has been saved.<br><form action=\"att_log_2.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } else { echo("<br><br>Error.....Email not sent and record not saved.<br><br>This may be because the Member doesn't have a trainor assigned to them.<br><form action=\"att_log_2.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>"); } } include("../php_include/close_all.php"); ?> Can anyone see any issues? I hope it makes sense. Many Thanks Link to comment https://forums.phpfreaks.com/topic/199773-script-taking-a-long-time-to-run-send-mail/ Share on other sites More sharing options...
taquitosensei Posted April 26, 2010 Share Posted April 26, 2010 first thing I noticed $num = mssql_num_rows($rs); if ( $num !=0 ) { if (mssql_num_rows($rs)) { could be shortened to if(mssql_num_rows($rs)!=0) { Link to comment https://forums.phpfreaks.com/topic/199773-script-taking-a-long-time-to-run-send-mail/#findComment-1048547 Share on other sites More sharing options...
New Coder Posted April 27, 2010 Author Share Posted April 27, 2010 Thanks taquitosensei nice tidy tip, I have made those changes and it didn't seem any quicker. I have however used some javascript to disable the button and display a task bar message to prevent re-clicks. I have also made the script only send one email other than multiple. heres my modified script: Again please check and state if any improvements can be made: On the fill in details page I have (snipet only): <script> window.status = 'Sending Email, Please Wait...'; document.formName.save.disabled = true; document.formName.action = 'save_att_log.php'; document.formName.submit(); </script> and then I have changed part of the save page to: if(mssql_num_rows($rs)!=0) { $re_staff = "An absence has been logged for $member_name ($member_id)"; $msg_staff = "A call has been logged for $member_name ($member_id) \n\nReason: $reason \n\nDate of Absence: $absent_date \n\nTime of Call: $call_time \n\nLog Date: $log_date \n\nLog Time: $log_time \n\nLogger: $login_id \n\nThank You"; $headers_staff = "From: No-Reply@our_domain.com \r\n"; while ($row = mssql_fetch_array($rs)) { $staff_email = $row["staffname"]. "@ourdomain.com,"; $to_staff .= $staff_email; } mail( $to_staff, $re_staff, $msg_staff, $headers_staff ); Not sure it's any quicker but at least the user can't resave the record. Link to comment https://forums.phpfreaks.com/topic/199773-script-taking-a-long-time-to-run-send-mail/#findComment-1049193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.