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.
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.