-
Notifications
You must be signed in to change notification settings - Fork 3
/
d8mail.module
63 lines (53 loc) · 1.9 KB
/
d8mail.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
use Drupal\Component\Utility\SafeMarkup;
/**
* Implements hook_entity_insert().
*/
function d8mail_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'article')) {
return;
}
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'd8mail';
$key = 'node_insert';
$to = \Drupal::currentUser()->getEmail();
$params['message'] = $entity->get('body')->value;
$params['node_title'] = $entity->label();
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ( ! $result['result']) {
$message = t('There was a problem sending your email notification to @email for creating node @id.', array('@email' => $to, '@id' => $entity->id()));
drupal_set_message($message, 'error');
\Drupal::logger('d8mail')->error($message);
return;
}
$message = t('An email notification has been sent to @email for creating node @id.', array('@email' => $to, '@id' => $entity->id()));
drupal_set_message($message);
\Drupal::logger('d8mail')->notice($message);
}
/**
* Implements hook_mail().
*/
function d8mail_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'node_insert':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('Node created: @title', array('@title' => $params['node_title']), $options);
$message['body'][] = SafeMarkup::checkPlain($params['message']);
break;
}
}
/**
* Implements hook_mail_alter().
*/
function d8mail_mail_alter(&$message) {
switch ($message['key']) {
case 'node_insert':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
break;
}
}