We have a script for sending emails, which can be few or many, depends on the need of the staff of our RH
, I am not able to solve the problem and satisfactorily treat the error message that is being presented and not exactly how to do it correctly, we have an authenticated account for the sending, what exactly happens is that at a certain moment the submission accuses error and I do not know if the submissions were executed or how to reverse those that were theoretically sent, of course , if at all possible.
The script that gets the checks
checked:
function EnviaEmail() {
var ntextoEmail = $('textarea[name="textoEmail"]').val();
var nChecked = new Array();
nChecked.push(<?php echo $id; ?>);
$("#email .modal-body p").html('<img src="../images/loading.gif" alt="Enviando..." />');
$("#email #btnEnviaEmail").hide();
var params = {
list: nChecked,
textoEmail: ntextoEmail
};
$.ajax({
url: "enviaEmail.php",
data: params,
dataType: "json",
type: "POST"
})
.done(function( json ) {
console.log(json);
if (json.flag == 1) {
var msg = '<div class="alert alert-success" style="width:450px;">' + json.msg + '</div>' ;
} else {
var msg = '<div class="alert alert-error" style="width:450px;">' + json.msg + '</div>' ;
}
$("#email .modal-body p").html(msg);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Requisicao falhou: " + err );
var msg = '<div class="alert alert-error" style="width:450px;">Falha na chamada. Tente novamente.</div>' ;
$("#email .modal-body p").html(msg);
});
}
And what is trying to send the emails:
require_once('../../Connections/conCurriculo.php');
// Recebendo o array com os ID´S
$checkboxes = $_POST['list'];
// Codificando a string para resolver problema com acentuação
$textoEmail = utf8_decode(nl2br($_POST['textoEmail']));
$arr = array();
$arr['flag'] = 0;
$arr['msg'] = 'Erro na execução';
// laço para buscar e-mail e efetuar envio
foreach($checkboxes as $id) {
// buscando informações do(s) candidato(s)
mysql_select_db($database_conCurriculo, $conCurriculo);
$query_rsRegistro = "SELECT * FROM candidato WHERE id_candidato = $id ";
$rsRegistro = mysql_query($query_rsRegistro, $conCurriculo) or die(mysql_error());
$row_rsRegistro = mysql_fetch_assoc($rsRegistro);
$totalRows_rsRegistro = mysql_num_rows($rsRegistro);
// dados do candidato
$nome = $row_rsRegistro['nome'];
$emailCandidato = $row_rsRegistro['email'];
//Dados pessoais
$emailDestinatario = addslashes("[email protected]");
$mensagem = addslashes($textoEmail);
$mensagem = eregi_replace("[\]",'',$mensagem);
require_once('../../phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Charset = 'ISO-8859-1';
$mail->SetLanguage("br");
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "usuario";
$mail->Password = "senha";
$mail->AddAddress($emailCandidato, "Recrutamento");
$mail->SetFrom($emailDestinatario,"Recrutamento");
$mail->Subject = 'Agendamento de Entrevista - ' . $nome;
$mail->MsgHTML($mensagem);
$status = $mail->Send();
} // fim do foreach
if ($status == 'TRUE') {
$arr['msg'] = 'Os e-mails foram enviados corretamente ';
$arr['flag'] = 1;
} else {
$arr['msg'] = 'Os e-mails não puderam ser enviados, por favor, tente novamente';
$arr['flag'] = 0;
}
$arr = array_map('htmlentities',$arr);
echo json_encode($arr);
//Limpa os destinatários
$mail->ClearAllRecipients();
$mail->ClearAttachments();
The error that is displayed falls on this part of the script:
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Requisicao falhou: " + err );
var msg = '<div class="alert alert-error" style="width:450px;">Falha na chamada. Tente novamente.</div>' ;
$("#email .modal-body p").html(msg);
})
Few emails are sent.