gmc1103 Posted September 28, 2015 Share Posted September 28, 2015 Hi I have an insert code into my database and if the result of the insert is ok an emal is sended to to admin but when i use this function (email) the user receives the json response after several seconds and not right away like he does when i don't use this function, anyone knows why? This is my code $sql = "SELECT COUNT( * ) as total_count FROM `new_visitas` WHERE `id_user`= ? AND `data_partida` = ? AND `hora_partida` = ?"; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('iss', $iduser, $data_partida, $hora_partida); $stmt->execute(); $stmt->bind_result($existe); $stmt->fetch(); $stmt->close(); if ($existe != 0) { $response = "Erro...Existe sobreposição nesse dia/hora para este utilizador."; echo json_encode(array('status' => 'error', 'message' => 'Erro...Existe sobreposição nesse dia/hora para este utilizador.')); } else { $sql = "INSERT INTO `new_visitas` (`id_user`, `local`, `data_partida`, `hora_partida`, `data_chegada`, `hora_chegada`, `turmas`, `disc`, `docentes`, `obj`, `tp`, `custo`, `total`, `itinerario`, `obs`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('issssssssssssss', $iduser, $newLocal, $data_partida, $hora_partida, $data_chegada, $hora_chegada, $newTurmas, $newDis, $newDocentes, $newObj, $newTp, $newCusto, $alunos, $newIti, $newObs); if (!$stmt->execute()) { echo json_encode(array('status' => 'error', 'message' => 'Opppss...A visita não foi registada')); } else { echo json_encode(array('status' => 'success', 'message' => 'A sua visita foi registada...Obrigado')); enviarEmail($newLocal,$data_partida, $hora_partida, $data_chegada, $hora_chegada, $newTurmas, $newDis, $newDocentes,$custo, $alunos); } $stmt->close(); } function enviarEmail($newLocal,$data_partida, $hora_partida, $data_chegada, $hora_chegada, $newTurmas, $newDis, $tp,$custo, $alunos){ $adress = "gilcarvalho@live.madeira-edu.pt"; $mail = new PHPMailer; $mail->isSMTP(); $mail->SMTPDebug = 3; $mail->Mailer = "smtp"; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = 465; $mail->SMTPKeepAlive = true; $mail->SMTPAuth = true; $mail->Username = "alphasil@gmail.com"; $mail->Password = "0942sv"; $mail->From = 'alphasil@gmail.com'; $mail->FromName = "EBSPMA - PAAD"; $mail->addReplyTo('alphasil@gmail.com', 'PAAD - Atividades'); $mail->AddAddress($adress, "PAAD"); $mail->IsHTML(true); $mail->Subject = 'Registo de nova visita de estudo'; $mail->CharSet = 'UTF-8'; $mail->Body = " <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> </head> <body> <h1>Visita de Estudo</h1> <p>Seguem os dados desta visita de estudo</p> <table> <tbody> <tr> <td><strong>Visita de Estudo</strong></td> <td>$newLocal</td> </tr> <tr> <td><strong>Data Partida</strong></td> <td>$data_partida</td> </tr> <tr> <td><strong>Hora Partida</strong></td> <td>$hora_partida</td> </tr> <tr> <td><strong>Data Chegada</strong></td> <td>$data_chegada</td> </tr> <tr> <td><strong>Hora Chegada</strong></td> <td>$hora_chegada</td> </tr> <tr> <td><strong>Turmas</strong></td> <td>$newTurmas</td> </tr> <tr> <td><strong>Disciplinas</strong></td> <td>$newDis</td> </tr> <tr> <td><strong>Transporte</strong></td> <td>$tp</td> </tr> <tr> <td><strong>Custo</strong></td> <td>$custo</td> </tr> <tr> <td><strong>Total Alunos</strong></td> <td>$alunos</td> </tr> </tbody> </table> <p> </p> <p>Aceda à sua área para validar esta visita de estudo e gerar os PDFs</p> <p> </p> <p>Equipa PAAD.</p> <p> </p> <p>Obrigado</p> </body> </html>"; if (!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; exit(); } else { exit(); } } Any recomendations to avoid this delay? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/298344-phpmailer-slow-down-issue/ Share on other sites More sharing options...
mac_gyver Posted September 28, 2015 Share Posted September 28, 2015 your would queue the email messages in a database table and use a cron job/scheduled task to actually send them. Quote Link to comment https://forums.phpfreaks.com/topic/298344-phpmailer-slow-down-issue/#findComment-1521719 Share on other sites More sharing options...
gmc1103 Posted September 28, 2015 Author Share Posted September 28, 2015 Thanks Can you give me an example , i never used this before Regards Quote Link to comment https://forums.phpfreaks.com/topic/298344-phpmailer-slow-down-issue/#findComment-1521722 Share on other sites More sharing options...
mac_gyver Posted September 28, 2015 Share Posted September 28, 2015 https://en.wikipedia.org/wiki/Web_search_engine Quote Link to comment https://forums.phpfreaks.com/topic/298344-phpmailer-slow-down-issue/#findComment-1521748 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.