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 = "
[email protected]";
$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 = "
[email protected]";
$mail->Password = "0942sv";
$mail->From = '
[email protected]';
$mail->FromName = "EBSPMA - PAAD";
$mail->addReplyTo('
[email protected]', '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