Skip to content
Snippets Groups Projects
Commit 7822ef6b authored by Balis Matzouranis's avatar Balis Matzouranis Committed by Adam Shepherd
Browse files

Issue #3262457 by balis_m, AdamPS: Cancel email sending

parent 9ba17278
No related branches found
No related tags found
1 merge request!15Added cancel sending email functionality
......@@ -134,6 +134,14 @@ symfony_mailer.email_adjuster_plugin.email_reply_to:
type: label
label: 'Display name'
symfony_mailer.email_adjuster_plugin.email_skip_sending:
type: mapping
label: 'Skip sending'
mapping:
message:
type: label
label: 'Skip message'
symfony_mailer.email_adjuster_plugin.email_subject:
type: mapping
label: 'Subject'
......
<?php
namespace Drupal\symfony_mailer\Exception;
/**
* Exception to cancel email sending.
*/
class SkipMailException extends \Exception {
}
......@@ -15,6 +15,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\Url;
use Drupal\symfony_mailer\Exception\MissingTransportException;
use Drupal\symfony_mailer\Exception\SkipMailException;
use Symfony\Component\Mailer\Mailer as SymfonyMailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
......@@ -133,7 +134,16 @@ class Mailer implements MailerInterface {
// to ensure it doesn't leak into the render context for the HTTP response
// to the current request.
return $this->renderer->executeInRenderContext(new RenderContext(), function () use ($email) {
return $this->doSend($email);
try {
return $this->doSend($email);
}
catch (SkipMailException $e) {
if ($this->account->hasPermission('administer mailer')) {
\Drupal::messenger()->addError($this->t('Email sending skipped: %message.', [
'%message' => $e->getMessage(),
]));
}
}
});
}
......
<?php
namespace Drupal\symfony_mailer\Plugin\EmailAdjuster;
use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Exception\SkipMailException;
/**
* Defines the Skip Sending Email Adjuster.
*
* @EmailAdjuster(
* id = "email_skip_sending",
* label = @Translation("Skip sending"),
* description = @Translation("Skips the email sending."),
* weight = -1,
* )
*/
class SkipSendingEmailAdjuster extends EmailAdjusterBase {
/**
* {@inheritdoc}
*/
public function preBuild(EmailInterface $email) {
throw new SkipMailException($this->configuration['message']);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['message'] = [
'#type' => 'textfield',
'#default_value' => $this->configuration['message'] ?? NULL,
'#description' => $this->t('Users with permission to manage mailer settings will see this message when skipping an email.'),
];
return $form;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment