Skip to content

Commit 9b2fd46

Browse files
committed
Added Drush command to send mail
1 parent 04f735b commit 9b2fd46

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
- [PR-391](https://github.com/itk-dev/os2loop/pull/391)
12+
Mail development setup
1113

1214
## [1.3.1]
1315

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
os2loop_mail_notifications.send_mail:
3+
class: Drupal\os2loop_mail_notifications\Command\SendMailCommand
4+
arguments:
5+
- "@plugin.manager.mail"
6+
tags:
7+
- { name: console.command }
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\os2loop_mail_notifications\Command;
6+
7+
use Drupal\Component\Utility\NestedArray;
8+
use Drupal\Core\Mail\MailManagerInterface;
9+
use Drupal\node\Entity\Node;
10+
use Symfony\Component\Console\Attribute\AsCommand;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Exception\InvalidArgumentException;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
18+
use Symfony\Component\Yaml\Yaml;
19+
20+
// phpcs:disable Drupal.Commenting.ClassComment.Missing
21+
#[AsCommand(
22+
name: 'os2loop_mail_notifications:mail:send',
23+
description: 'Send a mail',
24+
aliases: ['example'],
25+
)]
26+
final class SendMailCommand extends Command {
27+
28+
public function __construct(
29+
private readonly MailManagerInterface $mailManager,
30+
) {
31+
parent::__construct();
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
protected function configure() {
38+
$this
39+
->addArgument('to', InputArgument::REQUIRED, 'The mail recipient')
40+
->addOption('module', NULL, InputOption::VALUE_REQUIRED, 'The module', 'os2loop_flag_content')
41+
->addOption('key', NULL, InputOption::VALUE_REQUIRED, 'The key', 'flag_content')
42+
->addOption('langcode', NULL, InputOption::VALUE_REQUIRED, 'The langcode', 'en')
43+
->addOption('params', NULL, InputOption::VALUE_REQUIRED, 'The params (whatever that may be) as a Yaml object', '{}')
44+
->setHelp(<<<HELP
45+
Examples:
46+
47+
drush %command.name% test@example.com --params '
48+
message: My test message
49+
'
50+
HELP);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function execute(InputInterface $input, OutputInterface $output): int {
57+
$io = new SymfonyStyle($input, $output);
58+
59+
$to = $input->getArgument('to');
60+
$module = $input->getOption('module');
61+
$key = $input->getOption('key');
62+
$langcode = $input->getOption('langcode');
63+
try {
64+
$params = Yaml::parse($input->getOption('params'));
65+
}
66+
catch (\Exception $e) {
67+
throw new InvalidArgumentException(dt('Invalid params: %message', ['%message' => $e->getMessage()]));
68+
}
69+
70+
$defaultParams = match ($module) {
71+
'os2loop_flag_content' => [
72+
'reason' => 'reason',
73+
'message' => __FILE__,
74+
'node' => Node::create([
75+
'type' => 'test',
76+
'nid' => 0,
77+
'title' => __FILE__,
78+
]),
79+
],
80+
default => throw new InvalidArgumentException(dt('Unknown module: %module', ['%module' => $module])),
81+
};
82+
83+
$params = NestedArray::mergeDeep($defaultParams, (array) $params);
84+
85+
$send = TRUE;
86+
$result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
87+
88+
$success = $result['result'] ?? FALSE;
89+
if ($success) {
90+
$io->success(dt('Mail successfully sent to %to', ['%to' => $to]));
91+
}
92+
else {
93+
$io->error(dt('Error sending mail to %to', ['%to' => $to]));
94+
}
95+
96+
// Show the message data.
97+
$io->writeln(Yaml::dump($result));
98+
99+
return $success ? self::SUCCESS : self::FAILURE;
100+
}
101+
102+
}

0 commit comments

Comments
 (0)