It needs several things, I will summarize in topics:
Problem 1:
Change the PHP.ini:
You need to change to prevent PHP from terminating the process for time, this is only for security, because by default it will be zero, because it will run from the command line!
max_execution_time = 0;
Loop:
Create a while
and a sleep
within!
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 execute, for example, the command nohup
nohup php /caminho/para/o/arquivo.php &
nohup has the function of being able to run in the background, even if your connection to the server falls, this will make the script remain active even if there is an interruption in communication (or if you disconnect your device, for example).
Problem 2:
It lacks data on what the structure would look like, so I’m deducing that 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 make a loop in something scarce. That is, there is limit of emails that can be selected, so the "infinite" loop is unreal and is not important and will not be useful, since the files are not infinite.
However this does not seem to have been pointed out in the text or was not this exact question, 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 of the SSH client).
Must-read: PHP mail form doesn’t complete sending e-mail
– brasofilo