title: Send Emails behind the scene with Cron Jobs
body: <p>I have this method to notify "my followers" which is called after a new event has been added. It works but with more than one email makes all the process very slow since the popup waits that all the emails have been sent before showing to the user that the event has been added and the spinning wheel is going forever. I can't think of any way to do it "behind the scene" after the user get the message that the event has been added. </p>
  
  <pre><code>public function notifiedFollowers($creator_id, $type, $event_id){
     $query = $this-&gt;_db-&gt;prepare("SELECT * FROM followers WHERE user_id = ?");
     $query-&gt;bindParam(1, $creator_id, PDO::PARAM_INT);
     $query-&gt;execute();
  
  
        while($row = $query-&gt;fetch(PDO::FETCH_OBJ)) {
  
           $creator  = new User($creator_id);
           $creatorName = $creator-&gt;data()-&gt;name;
           $user = new User($row-&gt;followers_id);
           $mail = new PHPMailer();
           $template = New MailFactory();
           $mail-&gt;IsSMTP(); 
           $mail-&gt;Host = "myHost";  
           $mail-&gt;SMTPAuth = true; 
           $mail-&gt;Username = "myUser";
           $mail-&gt;Password = "myPassword";
           $mail-&gt;IsHTML(true);    
           $mail-&gt;From = 'from email';
           $mail-&gt;FromName = 'from name';
           $mail-&gt;AddAddress($user-&gt;data()-&gt;username); 
           $mail-&gt;Subject = 'add subject here';
           $mail-&gt;Body    = $template-&gt;notifiedFollowersEmail($creatorName, $user-&gt;data()-&gt;name, $type, $event_id);
              if(!$mail-&gt;send()) {
                echo $mail-&gt;ErrorInfo;
                die();
               }
        }
  }
  </code></pre>
  
  <p><em><strong></em>*UPDATE*<em>*</em>****</strong></p>
  
  <p>I solved the problem using cron jobs. Please check my answer.</p>