Commit 23240913 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3255835 by merlin06, AdamPS, axle_foley00: Send Test Email

parent f19bc837
Loading
Loading
Loading
Loading
+12 −0
Changes for config/install/symfony_mailer.mailer_policy.symfony_mailer.test.yml: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
langcode: en
status: true
dependencies: {  }
id: symfony_mailer.test
configuration:
  email_body:
    content:
      value: |-
        <p>This is a test email from <a href="[site:url]">[site:name]</a>.</p>
      format: email_html
  email_subject:
    value: 'Test email from [site:name]'

src/Form/TestForm.php

0 → 100644
+103 −0
Changes for src/Form/TestForm.php: 103 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\EmailFactory;
use Drupal\symfony_mailer\MailerHelperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Symfony Mailer test form.
 */
class TestForm extends FormBase {

  /**
   * The email factory service.
   *
   * @var \Drupal\symfony_mailer\EmailFactory
   */
  protected EmailFactory $emailFactory;

  /**
   * The mailer helper.
   *
   * @var \Drupal\symfony_mailer\MailerHelperInterface
   */
  protected $helper;

  /**
   * Constructs a new TestForm.
   *
   * @param \Drupal\symfony_mailer\EmailFactory $email_factory
   *   The email factory service.
   * @param \Drupal\symfony_mailer\MailerHelperInterface $helper
   *   The mailer helper.
   */
  public function __construct(EmailFactory $email_factory, MailerHelperInterface $helper) {
    $this->emailFactory = $email_factory;
    $this->helper = $helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('email_factory'),
      $container->get('symfony_mailer.helper'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'symfony_mailer_test_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#tree'] = TRUE;

    $form['description'] = [
      '#markup' => '<p>' . $this->t('This page allows you to send a test email to a recipient of your choice.') . '</p>',
    ];

    $form['recipient'] = [
      '#title' => $this->t('Recipient'),
      '#type' => 'textfield',
      '#default_value' => '',
      '#description' => $this->t('Recipient email address. Leave blank to send to yourself.'),
    ];

    $form['mailer_policy'] = $this->helper->renderTypePolicy('symfony_mailer');

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Send'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $to = $form_state->getValue('recipient') ?: $this->currentUser();
    $this->emailFactory->newModuleEmail('symfony_mailer', 'test')
      ->setTo($to)
      ->send();
    $message = is_object($to) ?
      $this->t('An attempt has been made to send an email to you.') :
      $this->t('An attempt has been made to send an email to @to.', ['@to' => $to]);
    $this->messenger()->addMessage($message);
  }

}
+30 −0
Changes for src/Plugin/EmailBuilder/TestEmailBuilder.php: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer\Plugin\EmailBuilder;

use Drupal\symfony_mailer\EmailFactoryInterface;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Processor\EmailBuilderBase;
use Drupal\symfony_mailer\Processor\TokenProcessorTrait;

/**
 * Defines the Email Builder plug-in for test mails.
 *
 * @EmailBuilder(
 *   id = "symfony_mailer",
 *   sub_types = { "test" = @Translation("Test email") },
 *   common_adjusters = {"email_subject", "email_body"},
 * )
 */
class TestEmailBuilder extends EmailBuilderBase {

  use TokenProcessorTrait;

  /**
   * {@inheritdoc}
   */
  public function fromArray(EmailFactoryInterface $factory, array $message) {
    return $factory->newModuleEmail($message['module'], $message['key']);
  }

}
+7 −1
Changes for symfony_mailer.links.task.yml: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ entity.mailer_policy.collection:
  route_name: entity.mailer_policy.collection

symfony_mailer.mailer_transport.collection:
  title: Transports
  title: Transport
  description: 'Listing of Mailer transports.'
  base_route: entity.mailer_policy.collection
  route_name: entity.mailer_transport.collection
@@ -16,6 +16,12 @@ symfony_mailer.import.status:
  base_route: entity.mailer_policy.collection
  route_name: symfony_mailer.import.status

symfony_mailer.test:
  title: Test
  description: 'Test Mailer.'
  base_route: entity.mailer_policy.collection
  route_name: symfony_mailer.test

entity.mailer_policy.edit_form:
  title: Edit
  route_name: entity.mailer_policy.edit_form
+9 −1
Changes for symfony_mailer.routing.yml: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ entity.mailer_transport.collection:
  path: '/admin/config/system/mailer/transport'
  defaults:
    _entity_list: 'mailer_transport'
    _title: 'Mailer'
    _title: 'Mailer transport'
  requirements:
    _permission: 'administer mailer'

@@ -107,3 +107,11 @@ symfony_mailer.import.skip:
    _title: 'Skip'
  requirements:
    _permission: 'administer mailer'

symfony_mailer.test:
  path: '/admin/config/system/mailer/test'
  defaults:
    _form: '\Drupal\symfony_mailer\Form\TestForm'
    _title: 'Mailer test'
  requirements:
    _permission: 'administer mailer'