You are here

function notify_mail in Notify 2.0.x

Same name and namespace in other branches
  1. 8 notify.module \notify_mail()
  2. 6 notify.module \notify_mail()
  3. 7 notify.module \notify_mail()
  4. 1.0.x notify.module \notify_mail()

Implements hook_mail().

File

./notify.module, line 149
Hooks for Notify module.

Code

function notify_mail($key, &$message, $params) {
  $host = \Drupal::request()
    ->getSchemeAndHttpHost();
  $config = \Drupal::config('notify.settings');
  $siteconfig = \Drupal::config('system.site');
  $username = $params['user']
    ->getDisplayName();
  $useruid = $params['user']
    ->id();
  $sitename = $siteconfig
    ->get('name');
  $sitemail = $siteconfig
    ->get('mail');
  $upl = $params['user']
    ->getPreferredLangcode();
  $messagefrom = $sitename . ' <' . $sitemail . '>';
  $message['headers']['MIME-Version'] = '1.0';

  // Need to do more to send nicely formatted text/html mail.
  // Stay at text/plain for now.
  // $message['headers']['Content-Type'] = 'text/html;charset=utf-8';.
  $message['headers']['Content-Type'] = 'text/plain;charset=utf-8';
  $message['headers']['From'] = $message['headers']['Sender'] = $message['headers']['Errors-To'] = $message['headers']['Reply-to'] = $messagefrom;
  $message['headers']['Return-Path'] = $sitemail;
  $message['subject'] = t('New content notification for @username from @sitename', [
    '@username' => $username,
    '@sitename' => $sitename,
  ], [
    'langcode' => $upl,
  ]);
  $message['body'][] = Markup::create(t('Greetings @user,', [
    '@user' => $username,
  ], [
    'langcode' => $upl,
  ]) . '<br /><br />');
  $message['body'][] = Markup::create(t('this is a notification about new content from @sitename.', [
    '@sitename' => $sitename,
  ], [
    'langcode' => $upl,
  ]) . '<br />');
  $fulltext = t('Click on the links below to see the whole node/comment.', [], [
    'langcode' => $upl,
  ]);
  $message['body'][] = t('This e-mail only lists the titles.', [], [
    'langcode' => $upl,
  ]) . ' ' . $fulltext;
  $message['body'][] = Markup::create($params['content']);
  $message['body'][] = Markup::create('<br />&nbsp;<br />' . t('This is an automatic e-mail from @sitename. To stop receiving these emails, change your notification preferences at @host/user/@useruid/notify.' . '</p>', [
    '@sitename' => $sitename,
    '@host' => $host,
    '@useruid' => $useruid,
  ], [
    'langcode' => $upl,
  ]));
}