1

I want to use php to send email to many e-mail addresses but:

  1. I want to make 30 seconds delay between every sent email
  2. I don't want limit to recipients list (my list is over 500 email) when I run the code it tells my time out after 30+ sent email

this is the code I use so far hope if you can help

<?php
$EmailsDB = file('emails.txt');
$from     = "myemail@website.com";
$fromName = "Website Name";
$subject  = "Subject";
$header = "MIME-Version: 1.0" . "\r\n"; 
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= 'From: '.$fromName.'<'.$from.'>' . "\r\n"; 
$message = file_get_contents("message.html");
            
foreach ($EmailsDB as $to) {
    if(mail($to, $subject, $message, $header)) {echo"E-mail sent successfully to $to <br />";}
    else {echo"Sorry, failed while sending!";}
}
?>

Hope to find the answer here, kind regards

  • 2
    This type of scripts should run from command line, so that you don't encounter the timeout error. If you can't use command line you can use `ini_set('max_execution_time', 0);` if your hosting provider allows it. But please read this beforehand: https://stackoverflow.com/questions/4306605/is-ini-setmax-execution-time-0-a-bad-idea – Andrea Olivato Mar 03 '22 at 11:45
  • Logic (not code ) : Transfer this .txt file to database, and create a new column default value to "0" and you will send the email one by one , when you send an email to recipient put +1 value to this column for each and create a cron task for 30 seconds. Thats all... And for prevent loop you can add sql code newcolumn!="plus1value". – Bbbb Mar 03 '22 at 12:06
  • No other options without making database? – Isaac Sabir Mar 03 '22 at 12:13
  • Just choose any solution you *can* do. Like a file of recepients you go through (maye JSON or CSV?) - whatever you want. However you have to do the saving/loading yourself. You can just decide how you want to solve it. – Peter Krebs Mar 03 '22 at 13:44

0 Answers0