diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bdd8002d..d68808224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- [PR-391](https://github.com/itk-dev/os2loop/pull/391) + Mail development setup ## [1.3.1] diff --git a/docker-compose.yml b/docker-compose.yml index 230ab5a47..cd2af25f2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,7 +38,7 @@ services: - PHP_MAX_EXECUTION_TIME=30 - PHP_MEMORY_LIMIT=256M # Depending on the setup, you may have to remove --read-envelope-from from msmtp (cf. https://marlam.de/msmtp/msmtp.html) or use SMTP to send mail - - PHP_SENDMAIL_PATH=/usr/bin/msmtp --host=mail --port=1025 --read-recipients --read-envelope-from + - PHP_SENDMAIL_PATH=/usr/bin/msmtp --host=mail --port=1025 --read-recipients - DOCKER_HOST_DOMAIN=${COMPOSE_DOMAIN:?} - PHP_IDE_CONFIG=serverName=localhost # Let drush know the site uri (makes using --uri redundant) diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/drush.services.yml b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/drush.services.yml new file mode 100644 index 000000000..e1eaad45c --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/drush.services.yml @@ -0,0 +1,7 @@ +services: + os2loop_mail_notifications.send_mail: + class: Drupal\os2loop_mail_notifications\Command\SendMailCommand + arguments: + - "@plugin.manager.mail" + tags: + - { name: console.command } diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Command/SendMailCommand.php b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Command/SendMailCommand.php new file mode 100644 index 000000000..0f115aba0 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Command/SendMailCommand.php @@ -0,0 +1,103 @@ +addArgument('to', InputArgument::REQUIRED, 'The mail recipient') + ->addOption('module', NULL, InputOption::VALUE_REQUIRED, 'The module', 'os2loop_flag_content') + ->addOption('key', NULL, InputOption::VALUE_REQUIRED, 'The key', 'flag_content') + ->addOption('langcode', NULL, InputOption::VALUE_REQUIRED, 'The langcode', 'en') + ->addOption('params', NULL, InputOption::VALUE_REQUIRED, 'The params (whatever that may be) as a Yaml object', '{}') + ->setHelp(<<getArgument('to'); + $module = $input->getOption('module'); + $key = $input->getOption('key'); + $langcode = $input->getOption('langcode'); + try { + $params = Yaml::parse($input->getOption('params')); + } + catch (\Exception $e) { + throw new InvalidArgumentException(dt('Invalid params: %message', ['%message' => $e->getMessage()])); + } + + // @todo Add some useful stuff here. + $defaultParams = match ($module) { + 'os2loop_flag_content' => [ + 'reason' => 'reason', + 'message' => __FILE__, + 'node' => Node::create([ + 'type' => 'test', + 'nid' => 0, + 'title' => __FILE__, + ]), + ], + default => throw new InvalidArgumentException(dt('Unknown module: %module', ['%module' => $module])), + }; + + $params = NestedArray::mergeDeep($defaultParams, (array) $params); + + $send = TRUE; + $result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); + + $success = $result['result'] ?? FALSE; + if ($success) { + $io->success(dt('Mail successfully sent to %to', ['%to' => $to])); + } + else { + $io->error(dt('Error sending mail to %to', ['%to' => $to])); + } + + // Show the message data. + $io->writeln(Yaml::dump($result)); + + return $success ? self::SUCCESS : self::FAILURE; + } + +} diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Plugin/Mail/PhpMail.php b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Plugin/Mail/PhpMail.php index e7a7e740d..86e96d744 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Plugin/Mail/PhpMail.php +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Plugin/Mail/PhpMail.php @@ -11,7 +11,7 @@ * * @Mail( * id = "os2loop_mail_notifications", - * label = @Translation("Custom PHP mailer"), + * label = @Translation("OS2Loop mail notifications"), * description = @Translation("Sends the message as plain text, using PHP's native mail() function.") * ) */