We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon cli process

I am new to phalcon..Trying to run background process for sending mail.... In terminal command "php app/cli.php main" is working properly ..But how can I make it to run from controller or how should I trigger this?? I need to send confirmation mail after each registration.. Thanks In advance



93.7k
Accepted
answer
edited May '18

There are multiple options:

  1. Execute the command from PHP: https://php.net/manual/bg/function.shell-exec.php
  2. Use a Queue technology, for example https://kr.github.io/beanstalkd/
  3. Cronjob that executes the file every given time.

Personally I use option 3. In my projects I have an email_queue table that holds the emails and a cronjob running every minute to send them.

CREATE TABLE `email_queue` (
  `id` int(11) UNSIGNED NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `subject` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `template` varchar(63) COLLATE utf8_unicode_ci NOT NULL,
  `variables` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'serialized key => value array',
  `priority` tinyint(1) NOT NULL COMMENT 'low 0 - 9 high',
  `created_at` datetime NOT NULL,
  `sent_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

So basically when a user registers, you insert a record in the DB and the cronjob does the work on the background.

edited May '18

Thank You sir @nikolay-mihaylov