A question would have some way of avoiding email resending (I am using the PHPMailer library) when reloading the page without using a button, since I am developing a notification system, but I have a problem that the notifications are they keep on the page until the users see them and change, then when the user reloads or re-enters the page and the notifications are still there, they send me the email again.
I tried to increment a variable each time the email was sent, and so when the variable is greater than 1, I did not send more emails, but when the page is reloaded it does not save the value of the incremented variable.
I appreciate your answers.
This is code:
<?php
$timeActual= time();
$hoy = date('Y-m-d');
$dia3 = date('Y-m-d', strtotime('+3 day'));
foreach($documentos as $user){
$fechaDB = $user->fecha_fin;
if ($fechaDB <> "0000-00-00") {
if( $fechaDB <= $hoy ) {
?>
<div class="container col-md-offset-0">
<div class="alert alert-info alert-dismissible col-lg-6">
<button type="button" class="close" data-dismiss="alert" aria-
hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alerta!</h4>
<?php echo "<b>CLIENTE: </b>".$user->nombre;?>
<br><b>NOTIFICACION: </b>Contrato terminado <?php echo " - "."<b>FECHA
FIN: </b>".$user->fecha_fin; ?>
</div>
</div>
<?php
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';//Modificar
$mail->Host = 'smtp.gmail.com';//Modificar
$mail->Port = '587';//Modificar
$mail->Username = '[email protected]'; //Modificar
$mail->Password = 'Aplicativomc'; //Modificar
$mail->setFrom('[email protected]', 'Sistema control de
facturación');//Modificar
$mail->addAddress('[email protected]', ' Sr. Omar Rincon');//
$mail->addAddress('[email protected]', ' Sr. Eduardo
Ramirez');//Modificar
$mail->IsHTML(true);
$html='
<html>
<head>
<title></title>
</head>
<body>
<h1>Mensaje de prueba</h1>
<p>Hola mundo XD</p>
<img
src="https://pbs.twimg.com/profile_banners/3140696879/1429203807/1500x500"
width="300" height="100">
</body>
</html>
';
$mail->Subject = 'Notificación';//Modificar
$mail->Body = $html; //Modificar
$mail->CharSet = 'UTF-8';
$intentos=0;
if($intentos == 0){
$intentos++;
$mail->send();
echo 'Enviado';
return true;
} else {
echo 'Error';
return false;
}
}else if ($fechaDB == $dia3){
...
?>
EDITION SOLUTION
Create a table called notifications with the fields ID, VALOR, ID_DOCUMENTOS
as the notifications are according to the date in the table NOTIFICATIONS I made the following query
$sql = "SELECT id, valor, id_documentos from notificacion WHERE id =
id and id_documentos = '$user->id'";
This query what it does is to bring me the data from the NOTIFICATIONS table according to the id_documents, and finally the condition
if($row["valor"] == 0){
$mail->send();
$sql = "INSERT INTO notificacion (valor, alerta, id_documentos) VALUES
(1,$user->id')";
$resultado = $mysqli->query($sql);
}else{
}
which looks at me if the alert that is assigned to that document has the value field in 0 ... if so send me the email and make the insert with the field sent in 1, then when I reload the page , as the field is in 1 it does not send me again.