Commit 3ac95546 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3276029 by AdamPS: Improve email creation

parent 1e958c0d
Loading
Loading
Loading
Loading
+15 −14
Changes for modules/symfony_mailer_bc/src/MailManagerReplacement.php: 15 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\symfony_mailer\EmailFactoryInterface;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\MailerHelperInterface;
use Drupal\symfony_mailer\Processor\EmailBuilderManagerInterface;

/**
 * Provides a Symfony Mailer replacement for MailManager.
@@ -46,6 +47,13 @@ class MailManagerReplacement extends MailManager {
   */
  protected $mailerHelper;

  /**
   * The email builder manager.
   *
   * @var \Drupal\symfony_mailer\Processor\EmailBuilderManagerInterface
   */
  protected $emailBuilderManager;

  /**
   * Constructs the MailManagerReplacement object.
   *
@@ -68,11 +76,14 @@ class MailManagerReplacement extends MailManager {
   *   The email factory.
   * @param \Drupal\symfony_mailer\MailerHelperInterface $mailer_helper
   *   The mailer helper.
   * @param \Drupal\symfony_mailer\Processor\EmailBuilderManagerInterface $email_builder_manager
   *   The email builder manager.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer, EmailFactoryInterface $email_factory, MailerHelperInterface $mailer_helper) {
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer, EmailFactoryInterface $email_factory, MailerHelperInterface $mailer_helper, EmailBuilderManagerInterface $email_builder_manager) {
    parent::__construct($namespaces, $cache_backend, $module_handler, $config_factory, $logger_factory, $string_translation, $renderer);
    $this->emailFactory = $email_factory;
    $this->mailerHelper = $mailer_helper;
    $this->emailBuilderManager = $email_builder_manager;
  }

  /**
@@ -87,22 +98,12 @@ class MailManagerReplacement extends MailManager {
      'params' => $params,
      'reply-to' => $reply,
      'send' => $send,
      'subject' => '',
      'body' => [],
    ];
    $entity = NULL;

    // Call alter hooks.
    $this->moduleHandler->alter(['mailer_bc', "mailer_bc_$module"], $message, $entity);

    if ($entity) {
      $email = $this->emailFactory->newEntityEmail($entity, $message['key']);
    }
    else {
      $email = $this->emailFactory->newModuleEmail($message['module'], $message['key']);
    }
    // Create an email from the array. Always call the plug-in from the module
    // name, not any variants.
    $email = $this->emailBuilderManager->createInstance($module)->fromArray($this->emailFactory, $message);

    $this->emailFromArray($email, $message);
    if ($message['send']) {
      $result = $email->send();
    }
+0 −40
Changes for modules/symfony_mailer_bc/src/Plugin/EmailBuilder/CommerceEmailBuilder.php: 0 added lines, 40 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;

use Drupal\symfony_mailer\Processor\EmailProcessorBase;
use Drupal\symfony_mailer\EmailInterface;

/**
 * Defines the Email Builder plug-in for commerce module.
 *
 * @EmailBuilder(
 *   id = "commerce",
 *   sub_types = { "order_receipt" = @Translation("Order receipt") },
 * )
 *
 * @todo Notes for adopting Symfony Mailer into commerce. It should be possible
 * to remove the MailHandler service. Classes such as OrderReceiptMail could
 * call directly to EmailInterface or even be converted to an EmailBuilder. The
 * commerce_order_receipt template could be retired, switching instead to use
 * email__commerce__order_receipt.
 */
class CommerceEmailBuilder extends EmailProcessorBase {

  /**
   * {@inheritdoc}
   */
  public function build(EmailInterface $email) {
    $email->setSubject($email->getParam('subject'))
      ->setBody($email->getParam('body'));

    if ($from = $email->getParam('from')) {
      $email->setFrom($from);
    }

    if ($bcc = $email->getParam('bcc')) {
      $email->setBcc($bcc);
    }
  }

}
+46 −1
Changes for modules/symfony_mailer_bc/src/Plugin/EmailBuilder/ContactEmailBuilder.php: 46 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@

namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;

use Drupal\contact\MessageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\symfony_mailer\Processor\EmailBuilderBase;
use Drupal\symfony_mailer\EmailFactoryInterface;
use Drupal\symfony_mailer\EmailInterface;

/**
@@ -20,15 +24,56 @@ use Drupal\symfony_mailer\EmailInterface;
 */
class ContactEmailBuilder extends ContactEmailBuilderBase {

  /**
   * Saves the parameters for a newly created email.
   *
   * @param \Drupal\symfony_mailer\EmailInterface $email
   *   The email to modify.
   * @param \Drupal\contact\MessageInterface $message
   *   Submitted message entity.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The sender.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The recipient.
   */
  public function createParams(EmailInterface $email, MessageInterface $message = NULL, AccountInterface $sender = NULL, AccountInterface $recipient = NULL) {
    assert($recipient != NULL);
    $email->setParam('contact_message', $message)
      ->setParam('sender', $sender)
      ->setParam('recipient', $recipient);
  }

  /**
   * {@inheritdoc}
   */
  public function fromArray(EmailFactoryInterface $factory, array $message) {
    $sender = $message['params']['sender'];
    $contact_message = $message['params']['contact_message'];

    // There are two separate email types, so no need for the key to contain
    // page_ or user_.
    $key = substr($message['key'], 5);

    if (isset($message['params']['contact_form'])) {
      return $factory->newEntityEmail($message['params']['contact_form'], $key, $contact_message, $sender);
    }
    return $factory->newModuleEmail('contact', $key, $contact_message, $sender, $message['params']['recipient']);
  }

  /**
   * {@inheritdoc}
   */
  public function build(EmailInterface $email) {
    parent::build($email);
    $recipient = $email->getParams()['recipient'];
    $recipient = $email->getParam('recipient');

    $email->setVariable('recipient_name', $recipient->getDisplayName())
      ->setVariable('recipient_edit_url', $recipient->toUrl('edit-form')->toString());

    if ($email->getSubType() == 'mail') {
      // Set the account from the recipient.
      $email->setAccount($recipient);
    }
  }

}
+9 −2
Changes for modules/symfony_mailer_bc/src/Plugin/EmailBuilder/ContactEmailBuilderBase.php: 9 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,14 +2,14 @@

namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;

use Drupal\symfony_mailer\Processor\EmailProcessorBase;
use Drupal\symfony_mailer\Processor\EmailBuilderBase;
use Drupal\symfony_mailer\EmailInterface;

/**
 * Defines a base class for contact module email builders.
 * )
 */
class ContactEmailBuilderBase extends EmailProcessorBase {
class ContactEmailBuilderBase extends EmailBuilderBase {

  /**
   * {@inheritdoc}
@@ -27,6 +27,13 @@ class ContactEmailBuilderBase extends EmailProcessorBase {
      ->setVariable('site_name', \Drupal::config('system.site')->get('name'))
      ->setVariable('sender_name', $sender->getDisplayName())
      ->setVariable('sender_url', $sender->isAuthenticated() ? $sender->toUrl('canonical')->toString() : $sender->getEmail());

    if ($email->getSubType() == 'mail') {
      $email->setReplyTo($sender->getEmail());
    }
    else {
      $email->setAccount($sender);
    }
  }

}
+20 −3
Changes for modules/symfony_mailer_bc/src/Plugin/EmailBuilder/ContactPageEmailBuilder.php: 20 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -3,11 +3,12 @@
namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;

use Drupal\contact\Entity\ContactForm;
use Drupal\contact\MessageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Entity\MailerPolicy;
use Drupal\symfony_mailer\MailerHelperTrait;
use Drupal\symfony_mailer\Processor\MailerPolicyImportInterface;

/**
 * Defines the Email Builder plug-in for contact module page forms.
@@ -27,10 +28,26 @@ use Drupal\symfony_mailer\Processor\MailerPolicyImportInterface;
 * @todo Notes for adopting Symfony Mailer into Drupal core. This builder can
 * set langcode, to, reply-to so the calling code doesn't need to.
 */
class ContactPageEmailBuilder extends ContactEmailBuilderBase implements MailerPolicyImportInterface {
class ContactPageEmailBuilder extends ContactEmailBuilderBase {

  use MailerHelperTrait;

  /**
   * Saves the parameters for a newly created email.
   *
   * @param \Drupal\symfony_mailer\EmailInterface $email
   *   The email to modify.
   * @param \Drupal\contact\MessageInterface $message
   *   Submitted message entity.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The sender.
   */
  public function createParams(EmailInterface $email, MessageInterface $message = NULL, AccountInterface $sender = NULL) {
    assert($sender != NULL);
    $email->setParam('contact_message', $message)
      ->setParam('sender', $sender);
  }

  /**
   * {@inheritdoc}
   */
@@ -44,7 +61,7 @@ class ContactPageEmailBuilder extends ContactEmailBuilderBase implements MailerP
      $email->setAccount();
    }
    if ($email->getSubType() == 'autoreply') {
      $email->setBody($email->getParam('contact_form')->getReply());
      $email->setBody($email->getEntity()->getReply());
    }
  }

Loading