You are here

public function Email::getNewResultsMail in Search API Saved Searches 8

Prepares a message containing new saved search results.

Parameters

array|\ArrayAccess $message: An array to be filled in. Elements in this array include:

  • id: An ID to identify the mail sent. Look at module source code or MailManagerInterface->mail() for possible id values.
  • to: The address or addresses the message will be sent to. The formatting of this string must comply with RFC 2822.
  • subject: Subject of the email to be sent. This must not contain any newline characters, or the mail may not be sent properly. MailManagerInterface->mail() sets this to an empty string when the hook is invoked.
  • body: An array of lines containing the message to be sent. Drupal will format the correct line endings for you. MailManagerInterface->mail() sets this to an empty array when the hook is invoked. The array may contain either strings or objects implementing \Drupal\Component\Render\MarkupInterface.
  • from: The address the message will be marked as being from, which is set by MailManagerInterface->mail() to either a custom address or the site-wide default email address when the hook is invoked.
  • headers: Associative array containing mail headers, such as From, Sender, MIME-Version, Content-Type, etc. MailManagerInterface->mail() pre-fills several headers in this array.

array|\ArrayAccess $params: An associative array with the following keys:

  • search: The saved search entity for which results are being reported.
  • results: A Search API result set containing the new results.

See also

hook_mail()

search_api_saved_searches_mail()

File

src/Plugin/search_api_saved_searches/notification/Email.php, line 436

Class

Email
Provides e-mails as a notification mechanism.

Namespace

Drupal\search_api_saved_searches\Plugin\search_api_saved_searches\notification

Code

public function getNewResultsMail(&$message, $params) {

  /** @var \Drupal\search_api_saved_searches\SavedSearchInterface $search */
  $search = $params['search'];
  $account = $search
    ->getOwner();

  /** @var \Drupal\search_api\Query\ResultSetInterface $results */
  $results = $params['results'];
  $data = [
    'search_api_saved_search' => $search,
    'search_api_results' => $results,
    'user' => $account,
  ];
  $options['langcode'] = $this
    ->getPreferredLangcode($search);
  $options['clear'] = TRUE;
  $subject = $this->configuration['notification']['title'];
  $subject = $this
    ->getTokenService()
    ->replace($subject, $data, $options);
  $body = $this->configuration['notification']['body'];
  $body = $this
    ->getTokenService()
    ->replace($body, $data, $options);
  $message['subject'] = $subject;
  $message['body'][] = $body;
}