Send emails backstage with Cron Jobs

I have this method for notifying "my followers", which is called after adding a new event. It works, but with several email messages the whole process is very slow, since the pop-up window expects all emails to be sent before showing the user that the event has been added and the spinning wheel will be forever. I can not think of any way to do this "behind the scenes" after the user receives a message that the event has been added.

public function notifiedFollowers($creator_id, $type, $event_id){ $query = $this->_db->prepare("SELECT * FROM followers WHERE user_id = ?"); $query->bindParam(1, $creator_id, PDO::PARAM_INT); $query->execute(); while($row = $query->fetch(PDO::FETCH_OBJ)) { $creator = new User($creator_id); $creatorName = $creator->data()->name; $user = new User($row->followers_id); $mail = new PHPMailer(); $template = New MailFactory(); $mail->IsSMTP(); $mail->Host = "myHost"; $mail->SMTPAuth = true; $mail->Username = "myUser"; $mail->Password = "myPassword"; $mail->IsHTML(true); $mail->From = 'from email'; $mail->FromName = 'from name'; $mail->AddAddress($user->data()->username); $mail->Subject = 'add subject here'; $mail->Body = $template->notifiedFollowersEmail($creatorName, $user->data()->name, $type, $event_id); if(!$mail->send()) { echo $mail->ErrorInfo; die(); } } } 

* UPDATE ******

I solved the problem using cron jobs. Please check my answer.

+1
source share
2 answers

I finally found the perfect solution: Cron Job

First of all, I created a database in which I save the β€œtask” to be completed

 CREATE TABLE IF NOT EXISTS `cron_jobs_new_event` ( `cron_job_id` int(32) NOT NULL AUTO_INCREMENT, `event_id` int(32) NOT NULL, `user_id` int(32) NOT NULL, `notify` int(32) NOT NULL, `executed` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`cron_job_id`), KEY `event_id` (`event_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Constraints for table `cron_jobs_new_event` -- ALTER TABLE `cron_jobs_new_event` ADD CONSTRAINT `cron_jobs_new_fk` FOREIGN KEY (`event_id`) REFERENCES `event` (`event_id`) ON DELETE CASCADE ON UPDATE CASCADE; 

I use hostgator, so I set up a cron job every 15 minutes: enter image description here

cron.php just call the method from my class Notification

 <?php require 'core/init.php'; $notification = new Notification(); $notification->notifiedFollowers(); 

and this is my method that does magic (at least for me ;-)

  public function notifiedFollowers(){ $query = $this->_db->prepare("SELECT user_id FROM cron_jobs_new_event WHERE executed = '0' AND notify = '1'"); $query->execute(); while($row = $query->fetch(PDO::FETCH_OBJ)) { $userCreator = $row->user_id; $q = $this->_db->prepare("SELECT * FROM followers WHERE user_id = ?"); $q->bindParam(1, $userCreator, PDO::PARAM_INT); $q->execute(); while($followers = $q->fetch(PDO::FETCH_OBJ)) { $type='try'; $creator = new User($followers->user_id); $creatorName = $creator->data()->name; $userFollower = new User($followers->followers_id); $mail = new PHPMailer(); $template = New MailFactory(); $mail->IsSMTP(); $mail->Host = "my host"; $mail->SMTPAuth = true; $mail->Username = "my username"; $mail->Password = "myPassword"; $mail->IsHTML(true); $mail->From = 'ev'; $mail->FromName = 'Events Team'; $mail->AddAddress($userFollower->data()->username); $mail->Subject = 'Somebody add a new event!'; $mail->Body = $template->notifiedFollowersEmail($creatorName, $userFollower->data()->name, $type, $followers->event_id); if(!$mail->send()) { echo $mail->ErrorInfo; die(); } } //here I update the database so next time the cron runs, It won't send the same message $update = $this->_db->prepare("UPDATE cron_jobs_new_event SET executed = '1' WHERE cron_job_id = ?"); $update->bindParam(1, $row->cron_job_id, PDO::PARAM_INT); $update->execute(); } } 

I hope this helps someone else

+2
source

You are looking for Themes

 <?php class AsyncEmail extends Thread { public function __construct($arg){ $this->arg = $arg; } public function run() { /** Add your email sending code here. **/ } } // and call this following lines in loop $aEmail = new AsyncEmail( $arg ); var_dump($aEmail->start()); ?> 

This code will work asynchronously in the background of your code, and your user will not have to wait for the completion of any step.

Take a look at this PHP streaming call for a php function asynchronously

+3
source

All Articles