You are here

class SendEmail in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesAction/SendEmail.php \Drupal\business_rules\Plugin\BusinessRulesAction\SendEmail

Class SendEmail.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "send_email",
  label = @Translation("Send email"),
  group = @Translation("System"),
  description = @Translation("Sent email action."),
  isContextDependent = FALSE,
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of SendEmail

File

src/Plugin/BusinessRulesAction/SendEmail.php, line 29

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction
View source
class SendEmail extends BusinessRulesActionPlugin {

  /**
   * The mail manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id = 'send_email', $plugin_definition = []) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->mailManager = $this->util->container
      ->get('plugin.manager.mail');
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $form_state
      ->set('business_rules_item', $item);

    // Only show settings form if the item is already saved.
    if ($item
      ->isNew()) {
      return [];
    }
    $site_mail = \Drupal::config('system.site')
      ->get('mail');
    $settings['use_site_mail_as_sender'] = [
      '#type' => 'select',
      '#title' => t('Use site mail as sender'),
      '#options' => [
        1 => t('Yes'),
        0 => t('No'),
      ],
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('use_site_mail_as_sender') ?: 0,
      '#description' => t('Use %mail as sender', [
        '%mail' => $site_mail,
      ]),
    ];
    $settings['from'] = [
      '#type' => 'textfield',
      '#title' => t('From'),
      '#default_value' => $item
        ->getSettings('from'),
      '#description' => t('You can use variables on this field.'),
      '#states' => [
        'visible' => [
          'select[name="use_site_mail_as_sender"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $settings['to'] = [
      '#type' => 'textfield',
      '#title' => t('To'),
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('to'),
      '#description' => t('For multiple recipients, use semicolon(;). You can use variables on this field. The variable can contain one email or an array of emails'),
    ];
    $settings['subject'] = [
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#required' => TRUE,
      '#maxlength' => 256,
      '#default_value' => $item
        ->getSettings('subject'),
      '#description' => t('You can use variables on this field.'),
    ];
    $settings['format'] = [
      '#type' => 'select',
      '#title' => t('Mail format'),
      '#options' => [
        'html' => t('HTML'),
        'text' => t('Text'),
      ],
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('format') ? $item
        ->getSettings('format') : 'text',
      '#description' => t('Email body format.'),
    ];
    $settings['body'] = [
      '#type' => 'text_format',
      '#title' => t('Message'),
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('body')['value'],
      '#description' => t('You can use variables on this field.'),
      '#format' => $item
        ->getSettings('body') && isset($item
        ->getSettings('body')['format']) ? $item
        ->getSettings('body')['format'] : 'full_html',
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    if (isset($settings['use_site_mail_as_sender']) && $settings['use_site_mail_as_sender'] === 1) {
      $settings['from'] = NULL;
    }
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {
    $event_variables = $event
      ->getArgument('variables');
    $to = $this
      ->processVariables($action
      ->getSettings('to'), $event_variables);
    $arr_to = explode(';', $to);
    $result = [];
    $from = NULL;

    // If we have set to NOT use site email; get the From setting.
    if (!$action
      ->getSettings('use_site_mail_as_sender')) {
      $from = $action
        ->getSettings('from');
      $from = $this
        ->processVariables($from, $event_variables);
    }

    // Should handle the To set as ; separated list: multiple emails sent
    // OR CSV list which is single item in $arr_to and 1 email sent to multiple people.
    foreach ($arr_to as $to) {

      // Handle RFC-822 formatted emails.
      $email_pattern = '/\\s*"?([^><,"]+)"?\\s*((?:<[^><,]+>)?)\\s*/';
      if (preg_match_all($email_pattern, $to, $matches, PREG_SET_ORDER) > 0) {
        foreach ($matches as $m) {
          if (!empty($m[2])) {
            $emails[trim($m[2], '<>')] = trim($m[1]);
          }
          else {
            $emails[$m[1]] = '';
          }
        }
      }
      else {

        // If not valid email or list of emails; just skip this one.
        continue;
      }

      // Check if $to (or 1st To) is a registered email to get Language.
      // @todo: go through all in CSV list to find a registered user.
      $user = user_load_by_mail(current(array_keys($emails)));
      if ($user) {
        $langcode = $user
          ->language()
          ->getId();
      }
      else {

        // If user not found, use the site language.
        $langcode = \Drupal::config('system.site')
          ->get('langcode');
      }

      // Send the email.
      $languageManager = \Drupal::languageManager();
      if ($languageManager instanceof ConfigurableLanguageManagerInterface) {
        $action_translated = $languageManager
          ->getLanguageConfigOverride($langcode, 'business_rules.action.' . $action
          ->id());
        $settings_translated = $action_translated
          ->get('settings');
      }
      $subject = isset($settings_translated['subject']) ? $settings_translated['subject'] : $action
        ->getSettings('subject');
      $message = isset($settings_translated['body']) ? $settings_translated['body'] : $action
        ->getSettings('body')['value'];
      $subject = $this
        ->processVariables($subject, $event_variables);
      $message = $this
        ->processVariables($message, $event_variables);

      // Check if body is on html format.
      if ($action
        ->getSettings('format') == 'html') {
        $headers['Content-Type'] = 'text/html; charset=UTF-8';
        $message = html_entity_decode($message);
      }
      else {
        $headers['Content-Type'] = 'text/plain; charset=UTF-8';
        $message = MailFormatHelper::htmlToText($message);
      }

      // Add we have our own From, add into headers. If not; Drupal will add Site Email
      if ($from) {
        $headers['From'] = $from;
      }
      $params = [
        'headers' => $headers,
        'subject' => $subject,
        'message' => $message,
      ];
      $send_result = $this->mailManager
        ->mail('business_rules', 'business_rules_mail', $to, $langcode, $params);
      $result = [
        '#type' => 'markup',
        '#markup' => t('Send mail result: %result. Subject: %subject, from: %from, to: %to, message: %message.', [
          '%result' => $send_result['result'] ? t('success') : t('fail'),
          '%subject' => $subject,
          '%from' => $from,
          '%to' => $to,
          '%message' => $message,
        ]),
      ];
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginInterface::validateForm 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SendEmail::$mailManager protected property The mail manager.
SendEmail::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
SendEmail::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
SendEmail::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase::processSettings
SendEmail::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides BusinessRulesItemPluginBase::__construct