If you want to code it yourself, here's a simple example for emailing a notification every time a Comment entity is posted (taken from an actual site with minor cleanup, but I might have broken something pulling out the site-specific code):
function mymodule_comment_insert(Comment $entity) {
mymodule__mail_notify_admin('new_comment', $entity, '', 'insert');
}
function mymodule_mail_notify_admin($key, $entity, $title, $moderation_state) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'mymodule';
$to_email = '[email protected]';
$path = $entity->toUrl('canonical', ['absolute' => TRUE])->toString();
$params['message'] = $path;
$params['title'] = $title;
$params['moderation_state'] = $moderation_state;
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$result = $mailManager->mail($module, $key, $to_email, $langcode, $params);
if ($result['result'] !== TRUE) {
$message = t('Error sending email notification to @email.', ['@email' => $to_email]);
\Drupal::logger('mymodule')->error($message);
return;
}
else {
$message = t('Email notification sent to @email', ['@email' => $to_email]);
\Drupal::logger('mymodule')->notice($message);
}
}
/*
* For reference:
* http://valuebound.com/resources/blog/how-to-send-mail-programmatically-drupal-8
*/
function mymodule_mail($key, &$message, $params) {
$message['from'] = \Drupal::config('system.site')->get('mail');
switch ($key) {
case 'new_comment':
// https://www.drupal.org/project/simple_comment_email_notification
$message['subject'] = t('New comment');
$message['body'][] = t('You can check the page at :unapprovedCommentsUrl for unapproved comments and :publishedCommentsUrl for published comments.', [
':unapprovedCommentsUrl' => \Drupal::request()->getSchemeAndHttpHost() . '/admin/content/comment/approval',
':publishedCommentsUrl' => \Drupal::request()->getSchemeAndHttpHost() . '/admin/content/comment',
]);
break;
default:
$options = [
'langcode' => $message['langcode'],
];
// @todo Fix HTML escaping.
// $message['body'][] = Html::escape($params['message']);.
$message['body'][] = $params['message'];
$message['subject'] = t('@title @ms on my site',
[
'@ms' => $params['moderation_state'],
'@title' => $params['title'],
],
$options);
break;
}
}