Skip to content

Commit 2757462

Browse files
authored
Merge pull request #391 from itk-dev/feature/mail-setup
Mail development setup
2 parents 6e70701 + 6a69a32 commit 2757462

5 files changed

Lines changed: 114 additions & 2 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

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ services:
3838
- PHP_MAX_EXECUTION_TIME=30
3939
- PHP_MEMORY_LIMIT=256M
4040
# 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
41-
- PHP_SENDMAIL_PATH=/usr/bin/msmtp --host=mail --port=1025 --read-recipients --read-envelope-from
41+
- PHP_SENDMAIL_PATH=/usr/bin/msmtp --host=mail --port=1025 --read-recipients
4242
- DOCKER_HOST_DOMAIN=${COMPOSE_DOMAIN:?}
4343
- PHP_IDE_CONFIG=serverName=localhost
4444
# Let drush know the site uri (makes using --uri redundant)
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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
// @todo Add some useful stuff here.
71+
$defaultParams = match ($module) {
72+
'os2loop_flag_content' => [
73+
'reason' => 'reason',
74+
'message' => __FILE__,
75+
'node' => Node::create([
76+
'type' => 'test',
77+
'nid' => 0,
78+
'title' => __FILE__,
79+
]),
80+
],
81+
default => throw new InvalidArgumentException(dt('Unknown module: %module', ['%module' => $module])),
82+
};
83+
84+
$params = NestedArray::mergeDeep($defaultParams, (array) $params);
85+
86+
$send = TRUE;
87+
$result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
88+
89+
$success = $result['result'] ?? FALSE;
90+
if ($success) {
91+
$io->success(dt('Mail successfully sent to %to', ['%to' => $to]));
92+
}
93+
else {
94+
$io->error(dt('Error sending mail to %to', ['%to' => $to]));
95+
}
96+
97+
// Show the message data.
98+
$io->writeln(Yaml::dump($result));
99+
100+
return $success ? self::SUCCESS : self::FAILURE;
101+
}
102+
103+
}

web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Plugin/Mail/PhpMail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @Mail(
1313
* id = "os2loop_mail_notifications",
14-
* label = @Translation("Custom PHP mailer"),
14+
* label = @Translation("OS2Loop mail notifications"),
1515
* description = @Translation("Sends the message as plain text, using PHP's native mail() function.")
1616
* )
1717
*/

0 commit comments

Comments
 (0)