Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. /**
  2. * Implements hook_mail().
  3. */
  4. function ipnotification_mail($key, &$message, $params) {
  5. $options = array(
  6. 'langcode' => $message['langcode'],
  7. );
  8. switch ($key) {
  9. case 'entity_create':
  10. $message['from'] = Drupal::config('system.site')->get('mail');
  11. $message['subject'] = $this->t('%entity created', ['%entity' => $params['title']], $options);
  12. $message['body'][] = Html::escape($params['message']);
  13. break;
  14. case 'entity_delete':
  15. $message['from'] = Drupal::config('system.site')->get('mail');
  16. $message['subject'] = $this->t('%entity deleted', ['%entity' => $params['title']], $options);
  17. $message['body'][] = $params['message'];
  18. break;
  19. case 'entity_update':
  20. $message['from'] = Drupal::config('system.site')->get('mail');
  21. $message['subject'] = $this->t('%entity updated', ['%entity' => $params['title']], $options);
  22. $message['body'][] = $params['message'];
  23. break;
  24. case 'user_login':
  25. $message['from'] = Drupal::config('system.site')->get('mail');
  26. $message['subject'] = $this->t('User has logged in', $options);
  27. $message['body'][] = $params['message'];
  28. break;
  29. case 'user_logout':
  30. $message['from'] = Drupal::config('system.site')->get('mail');
  31. $message['subject'] = $this->t('User has logged out', $options);
  32. $message['body'][] = $params['message'];
  33. break;
  34. }
  35. }
  36.  
  37. /**
  38. * Send email to site admin.
  39. *
  40. * @param string $key
  41. * The mail key to check on 5 available entity_create, entity_delete, entity_update, user_login, user_logout.
  42. * @param string $entity_label
  43. * The label of the current entity.
  44. * @param string $link
  45. * The link where stuff happened.
  46. */
  47. public function sendMail($key, $entity_label, $link) {
  48. $module = 'ipnotfiication';
  49. $to = Drupal::config('system.site')->get('mail');
  50. $params['title'] = $entity_label;
  51. $params['user'] = $this->user->getAccountName();
  52. $params['date'] = date('Y-m-d H:i:s');
  53. $params['mail'] = $this->user->getEmail();
  54. $params['link'] = $link;
  55. if (strpos('entity', $key)) {
  56. if (strpos('create', $key)) {
  57. $params['message'] = $this->t('User %user created Entity %link: %title',
  58. array('%title' => $params['title'], '%link' => $params['link']));
  59. }
  60. if (strpos('delete', $key)) {
  61. $params['message'] = $this->t('User %user deleted Entity %link: %title',
  62. array('%title' => $params['title'], '%link' => $params['link']));
  63. }
  64. if (strpos('update', $key)) {
  65. $params['message'] = $this->t('User %user updated Entity %link: %title',
  66. array('%title' => $params['title'], '%link' => $params['link']));
  67. }
  68. }
  69. else {
  70. if (strpos('login', $key)) {
  71. $params['message'] = $this->t('User %user has logged in on %date ',
  72. array(
  73. '%user' => $params['user'],
  74. '%date' => $params['date'],
  75. ));
  76. }
  77. else {
  78. $params['message'] = $this->t('User %user has logged out on %date ',
  79. array(
  80. '%user' => $params['user'],
  81. '%date' => $params['date'],
  82. ));
  83. }
  84. }
  85. $langcode = $this->user->getPreferredLangcode();
  86. $send = true;
  87. $result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
  88.  
  89. if ($result['result'] !== true) {
  90. drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  91. }
  92. else {
  93. drupal_set_message(t('Your message has been sent.'));
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement