//This file is used to store emails in to queue and access emails from queue /** * Class Queue */ class Queue { /** * Path to the saved file * @var string */ protected $path; /** * Queue constructor. * @param $path */ public function __construct($path) { $this->path = $path; } /** * Send an item to queue * * @param mixed $content - content to be send * @return mixed - number of bytes written to the queue, or false on failure */ public function push($content) { //this will create file in the path that you provided //EX: /Applications/MAMP/htdocs/queue/5a36b3db6bd933.13845696 $file = $this->path . uniqid('', true); $data = serialize($content); //Writing content that you passed to the 5a36b3db6bd933.13845696 file return file_put_contents($file, $data); } /** * This function will return all the items in the email queue list */ public function getNextItem() { $fileNames = scandir($this->path); $fileNames = array_diff($fileNames, ['.', '..']); $fileName = array_shift($fileNames); if ($fileName !== null) { $file = $this->path . $fileName; $cOntents= file_get_contents($file); if ($contents !== false) { $object = unserialize($contents); if ($object !== false) { unlink($file); return $object; } } } } }
//This file is used to aut load all the classes that we need in this task { "require": { "phpmailer/phpmailer": "5.*" }, "autoload": { "classmap": [ "classes" ] } }
# ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday; # │ │ │ │ │ 7 is also Sunday on some systems) # │ │ │ │ │ # │ │ │ │ │ # * * * * * command_to_execute ######crontab to process emails in queue ###### 7 8 * * 4 php /Applications/MAMP/htdocs/public/process-queue.php
Send an email
//This file is used to process all emails in teh queue require dirname(__DIR__).'/vendor/autoload.php'; $dir = dirname(__DIR__) . '/queue/'; $queue = new Queue($dir); $mail = $queue->getNextItem(); while ($mail !== null) { $mail->isSMTP(); $mail->Host = Config::SMTP_HOST; $mail->Port = Config::SMTP_PORT; $mail->SMTPAuth = true; $mail->Username = Config::SMTP_USER; $mail->Password = Config::SMTP_PASSWORD; $mail->SMTPSecure = 'tls'; $mail->CharSet = 'UTF-8'; if ($mail->send()) { echo "message send \n"; } else { echo "Mailer error" . $mail->ErrorInfo . "\n"; } }
//This is used to send email in to queue /** * Start the timer */ $start_time = microtime(true); /*Autoload file from phpmailer library you can download this library from composer and our custom classes*/ require "../vendor/autoload.php"; //Enable errors to display ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); //creating object for PHPMailer class in library $mail = new PHPMailer(); try { //configuration to send email $mail->isSMTP(); $mail->Host = Config::SMTP_HOST; $mail->Port = Config::SMTP_PORT; $mail->SMTPAuth = true; $mail->Username = Config::SMTP_USER; $mail->Password = Config::SMTP_PASSWORD; $mail->SMTPSecure = 'tls'; $mail->CharSet = 'UTF-8'; //Allow to display more debugging information from phpmailer //$mail->SMTPDebug = 2; //Send an email $mail->setFrom('phpmailmail@gmail.com', 'gopibabu'); $mail->addAddress('gopinyca.php@gmail.com'); $mail->Subject = 'Testing PHPMailer Messages asynchronous'; $mail->Body = 'Sending email asynchronously'; /** * Add email to queue */ $dir = dirname(__DIR__).'/queue/'; $queue = new Queue($dir); $queue->push($mail); } catch (Exception $e) { echo 'Message could not be queued.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } $end_time = microtime(true); $time = number_format($end_time - $start_time, 5); /** * Return to index.php */ header("Location:emailIndex.php?time=$time"); exit();