0

I am trying to automate a task. Which is sending emails automatically every week to some people. I created a MailController, inside it implemented a function which sends the emails. What i want to do now is to make my app call the function automatically every week or month.

What i tried is to implement a new command which would execute my function and then make a script to repeat that command.

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


    class MailCommand extends Command {

        protected function configure () {
            $defaultName = 'app:mailAuto';

            $this->setDescription("Permet d'envoyer des mails automatiquement lorsque le parc n'est pas à jour");

            $this->setHelp("Je serai affiche si on lance la commande bin/console app:mailAuto -h");

        }

        public function execute (InputInterface $input, OutputInterface $output) {
            // $controller = new MailController();
            // $controller->warningMailAction();
            $output->writeln('coucou !');
        }
    }

When executing the command i created i get this messages:

In FileLoader.php line 166:
  The autoloader expected class "App\Command\commandeMailAuto" to be defined
  in file "/var/www/html/ParcAuto/vendor/composer/../../src/Command/commandeM
  ailAuto.php". The file was found but the class was not in it, the class nam
  e or namespace probably has a typo in /var/www/html/ParcAuto/config/service
  s.yaml (which is loaded in resource "/var/www/html/ParcAuto/config/services
  .yaml").

In DebugClassLoader.php line 213:
  The autoloader expected class "App\Command\commandeMailAuto" to be defined
  in file "/var/www/html/ParcAuto/vendor/composer/../../src/Command/commandeM
  ailAuto.php". The file was found but the class was not in it, the class nam
  e or namespace probably has a typo.

I'm running symfony 4.3.2, the controller i'm trying to call extends AbstractController, my services.yaml is the default one. I think i missed some configurations but i can't find out which exactly. Thank you for your help, tell me if you need more information.

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
Tom.H
  • 11
  • 2
  • Is the shown code in `commandeMailAuto.php` ? – Cid Jul 19 '19 at 14:33
  • Indeed it is, sorry for the delay the same level as Controller, Entity ... – Tom.H Jul 22 '19 at 06:49
  • Then either rename the file `MailCommand.php` or the class `commandeMailAuto`. Symphony expect the class `foo` to be in the file `foo.php` for autoloading and expect the file `foo.php` to contain the class `foo` – Cid Jul 22 '19 at 07:34
  • It seems to have solved my first problem, thank you @Cid. I now have a problem about the name even though i defined it. `[WARNING] Some commands could not be registered:` `The command defined in "App\Command\mailCommand" cannot have an empty name.` `There are no commands defined in the "app" namespace.` `Did you mean this? doctrine:mapping` – Tom.H Jul 22 '19 at 07:59
  • Ah, adding the line: `$this->setName($defaultName)` seems to have solved the issue, my next step is to be able to call the function inside my controller. I have a MailController.php which defines a warningMailAction, my goal is to call that action with the command. – Tom.H Jul 22 '19 at 08:26

0 Answers0