You are here

public function SendEmail::execute in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/SendEmail.php \Drupal\business_rules\Plugin\BusinessRulesAction\SendEmail::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

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

Class

SendEmail
Class SendEmail.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

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;
}