It needs several things, I'll summarize in topics:
Problem 1:
Change PHP.ini:
It is necessary to change to prevent PHP from closing the process by time, this is only for security, because by default it will be zero, because it will be executed from the command line!
max_execution_time = 0;
Loop:
Create a while
and a sleep
inside!
while(1 === 1){ // 1 sempre será 1, sempre irá continuar!
// código
sleep(30); // 30 segundos de pausa!
}
Start this loop:
You must have access to SSH (or similar) and run, for example, the command nohup
nohup php /caminho/para/o/arquivo.php &
Nohup has the ability to run in the background, even if your connection to the server goes down, this will cause the script to remain active even if communication is interrupted (or if you disconnect the device, for example). example).
Problem 2:
It lacks data on what the structure would look like, so I'm guessing it would be an email per line, similar to the database!
<?PHP
require_once('class.phpmailer.php');
$i = 0;
while(1 === 1){ // LOOP
$arquivo = file('arquivo'.$i.'.txt');
// Seleciona o arquivo por linha, cada linha uma array.
// Isso irá selecionar o "arquivo0.txt" ao terminar será "arquivo1.txt", depois "arquivo2.txt", caso queria mante-lo fixo apenas remova o $i.
$f = 1; // numero de email
foreach($arquivo as $e){
//nada modificado
$mail = new PHPMailer();
$body = file_get_contents('a.html');
$mail->AddReplyTo("[email protected]","EREA SSA");
$mail->SetFrom('[email protected]', 'EREA SSA');
$mail->AddAddress($e, utf8_decode("$nome[$loop] $sobrenome[$loop]"));
$mail->Subject = utf8_decode("Sua inscrição foi aceita !");
$mail->MsgHTML($body);
//$mail->AddAttachment("edital.pdf"); // Arquivos para anexo
if(!$mail->Send()) {
echo "Erro: " . $mail->ErrorInfo . "<br/>";
} else {
echo "Mensagem enviada !<br/>";
}
$mail->clearAllRecipients();
//modificado para adicionar sleep
if($f %10 == 0) { // se for divido por 10 da pausa (10, 20, 30...)!
sleep(30);
}
$f++; // acrescenta +1 email
}
sleep(30); // por segurança após o foreach
$i++;
}
?>
Logical problem:
What you want is to loop in something scarce. That is, there is a limit of emails that can be selected, so the "infinite" loop is unreal and not important and will not be useful, since the files are not infinite.
However, this does not appear to have been pointed out in the text or it was not this exact issue, but it is the great point to consider. But the "Problem 1" solution is valid, including to prevent PHP from terminating the process before completion (or in case of disconnection from the SSH client).