I have this function that whenever an admin posts a new announcement, it sends an email to all the users of the forum:
$currentid = Yii::app()->db->createCommand("select id from content where id = (select max(id) from content)")->queryRow();
Yii::app()->session['announcement_message'] = 'You have successfully created an announcement.';
$url = Yii::app()->createAbsoluteUrl('announcement/view/id/'. $currentid["id"]);
if(Yii::app()->partyroles->isAdmin())
{
$this->emailAll($url);
}
$this->redirect(array('view','id'=>$model->id));
the emailAll() is another function that I made to do the actual mail sending but since there could be 100's to 1000's of emails in the future, the sending would be slow! Whenever I click the "create" button, it usually takes around 30 seconds before it redirects back to the view page since it takes a while to send all of those emails.
My question is if there's any way to speed this up? Or is there any clever way to perhaps show the user that "Email's are being sent, please wait?"
For such a number of recipients I would not send the emails from the script that creates the newsletter, but write filter for the emails and the other informations as job into a queue, which is then read by another process (e.g. triggered by cron job) that builds the list of emails using the filter and then sends the email. You could store the job with an id in the queue so that you could check with e.g. ajax if the job is done.
This will additionally allow your system to schedule the sending of emails if it is for some reason busy.