Jump to content

Cron and send mail


everisk

Recommended Posts

Hi,

 

I have a script that send a lot of emails from database and cron to make sure that it continues to send even after the script time out or stop running for some reason. However, how do I make sure that the email will not get sent twice? I think it is possible that the script is still running and when cron kicks in the same email might be fetch from database and that person will get the email message twice? Although I might delete the email address after it has been sent to but I think it is still possible that before it was successfully sent to, the cron kicks in and fetch the same email because the sending hasn't finished and therefore the email address hasn't been deleted.

 

I'm still at the concept part so any thoughts would be greatly aprreciated. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/150266-cron-and-send-mail/
Share on other sites

got it lol

 

 

so the email that are sent need to be put in a session array

then cheeked against the cron emails.

 

 

 

<?php

$testing=array("[email protected]","email3.com");// incoming emails from a session[] array.

$test=array("[email protected]","[email protected]","[email protected]");// emails being re sent via cron

//foreach the new emails the cron sending.
foreach($test as $sent_email){

// cheek if there a match with the session[] array.
if(in_array($sent_email,$testing)){

	break; // stop those matched emails.
}

echo "$sent_email<br>"; // echo unmatched emails.

}

?>

 

 

<?php

$ready_sent_emails=array("[email protected]","[email protected]","[email protected]");

foreach($ready_sent_emails as $sent_you){

$_SESSION['emails'][]=$sent_you;
}

// let be tend, the emails are sent above, via the emai function.

$testing[]=$_SESSION['emails'][0];

print_r($testing);

$test=array("[email protected]","[email protected]","[email protected]");

foreach($test as $sent_email){


if(in_array($sent_email,$testing)){

	break;
}

echo "$sent_email<br>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/150266-cron-and-send-mail/#findComment-789147
Share on other sites

sorry re wrote the code as your see it works.

<?php session_start();

$ready_sent_emails=array("[email protected]","[email protected]");

foreach($ready_sent_emails as $sent_you){
   
   $_SESSION['emails'][]=$sent_you;
}

// let be tend, the emails are sent above, via the emai function.

$testing=$_SESSION['emails'];


//database sending all emails again via cron.
$test=array("[email protected]","[email protected]","[email protected]");

foreach($test as $sent_email){


   if(in_array($sent_email,$testing)){
      
      break;
   }
   
   echo "$sent_email<br>"; // this can now send the email that has not been sent yet.

}

?>

Link to comment
https://forums.phpfreaks.com/topic/150266-cron-and-send-mail/#findComment-789160
Share on other sites

Thank you for your reply!

 

A few questions, though. How could the session be available to another instance of the script? I thought session is tie with session id and i think if i run the script in cron, new session id is being created. And suppose I send 100K of emails the session variable will get soo big?!

Link to comment
https://forums.phpfreaks.com/topic/150266-cron-and-send-mail/#findComment-789313
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.