pause of 2 seconds when sending email

Asked

Viewed 41 times

0

The duplicate question showed how to send and not be identified as spam.. now would like to do a Sleep between submissions ...

I use swiftmailer to send emails and would like to have a delay in sending 2 seconds per email sent.. I’ve tried using Sleep(2); in the loop like this:

foreach($users as $user) {sleep(2);
  $message->setTo($user["email"], $user["nome"]);
  $mailer->send($message);

but it doesn’t work... sending is processed all at once after 2 seconds and should be 2 seconds per email...any idea ? obg ";

// Create the mail transport configuration
$transport = Swift_SmtpTransport::newInstance("xxx.xxx.com", 25);
$transport->setUsername("xxx");
$transport->setPassword("xxx");


// Usually you want to replace the following static array
// with a dynamic one built retrieving users from the database
$users = unserialize($_POST['result']);





// Create the replacements array
$replacements = array();
foreach ($users as $user) {
  $replacements[$user["email"]] = array (
    "{nome}" => $user["nome"],
    "{total}" => $user["total"]
  );

}

// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);

// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("Pedido de recibos TopHospedeiras");
//$message->setCc('[email protected]');


// $message->setBody("msg a enviar

");


$message->setFrom("[email protected]", "Tophospedeiras");
// Send the email
foreach($users as $user) {
  $message->setTo($user["email"], $user["nome"]);
  $mailer->send($message);




  $ne++;
  echo "</br>n:".$ne."&nbsp".$user["email"]."</br> >>enviado</br>";

}
  • where you are setting the Sleep?

  • I tried inside the loop : foreach($users as $user) { Sleep(2); but n gave

  • It’s... strange that, because Sleep is the function that performs what you really want, the correct thing would be to put Sleep before the $mailer->send($message);, test using function in microseconds usleep(2000000);.

  • also did not give .. is paused

  • Of a echo date('h:i:s') . "\n"; before $mailer->send($message), after he gives the sleep(2), and after the Sleep of another one echo date('h:i:s') . "\n";, compare the times and see if paused... This function has no secret, would not have a why not work kkk

  • I think it worked, just not showing the echo one by one

  • when it finishes it shows that it paused but showed everything together at the end

  • Or use cron to send an email every X minutes.

  • @I-amSam, it shows everything together because unless you say otherwise, PHP output will only be done after all the script is processed. That doesn’t mean the Sleep isn’t working. In short: emails are being sent within 2 seconds of Sleep, but echo will be displayed at once.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.