9K Views

How to Refresh Mailbox Using Cron Job in UVdesk OpenSource

In this blog we will discuss how you can automate the periodic execution of UVDesk Mailbox refresh commands. To implement this, we will utilise cron job scheduling.

To start off, go inside the the mailbox-component bundle of your Open-Source UVDesk project, and open the Console sub-directory, in which you will find the file; RefreshMailboxCommand.php (located at project_root/vendor/uvdesk/mailbox-component/Console/RefreshMailboxCommand.php).

class RefreshMailboxCommand extends Command
{
    private $container;
    private $entityManager;

    public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager)
    {
        $this->container = $container;
        $this->entityManager = $entityManager;

        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('uvdesk:refresh-mailbox');
        $this->setDescription('Check if any new emails have been received and process them into tickets');

        $this->addArgument('emails', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, "Email address of the mailboxes you wish to update");
        $this->addOption('timestamp', 't', InputOption::VALUE_REQUIRED, "Fetch messages no older than the given timestamp");
    }

Now, override the parent class function execute, this is where all the logic will be written and executed.

protected function execute(InputInterface $input, OutputInterface $output)
{
//Your logic inside here...
}
To Run cron job in uvdesk opensource you just need to run below command:
crontab -e

Add the following cron expression to the crontab for automatic Mailbox Refreshing, (in the command below, the command will execute every 30 minute):

*/30 * * * * php /project_root_folder_path/bin/console uvdesk:refresh-mailbox your_mailbox_id
Now, if you are new to cron scheduling, below is how you can set the periodic execution timing to your exact requirement://The format is as follows:

+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed

I have explored this while contributing to the open-source Symfony based project UVDesk, there are a lot more things to learn and develop, feel free to contribute to an enterprise-level open-source helpdesk.

Category(s) Symfony UVdesk
. . .

Comments (2)

Add Your Comment

  • Vitaly
    In my case, I just added ‘php /path_to_project_folder/bin/console uvdesk:refresh-mailbox your_mailbox_id’
    to crontab and everything works fine.
    So what is the purpose of function ‘execute’ overridding and the line ‘php /project_root_forlder_path uvdesk:refresh-mailbox’ in your crontab expression?
    • Himani Gupta
      Hello Vitaly,
      Nice day to you!!
      what is the purpose of function ‘execute’ overridding and the line ‘php /project_root_folder_path uvdesk:refresh-mailbox’ in your crontab expression?
      >> The execute function associated with the community project script not crontab expression. You can go with the user guide to setup cronjob and start delivering excellent support.
      Still, you have a further issue or any kind of doubt, reach out to us at support@uvdesk.com
      Kind Respects
      UVdesk Team
  • css.php