Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Drupal\os2loop_mail_notifications\Command;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;

// phpcs:disable Drupal.Commenting.ClassComment.Missing
#[AsCommand(
name: 'os2loop_mail_notifications:mail:send',
description: 'Send a mail',
aliases: ['example'],
)]
final class SendMailCommand extends Command {

public function __construct(
private readonly MailManagerInterface $mailManager,
) {
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure() {
$this
->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(<<<HELP
Examples:

drush %command.name% test@example.com --params '
message: My test message
'
HELP);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);

$to = $input->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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
* )
*/
Expand Down
Loading