Message Notify
Provides a plugin-based notification framework for sending Message entities via various delivery channels such as email and SMS.
message_notify
Install
composer require 'drupal/message_notify:8.x-1.1'
composer require 'drupal/message_notify:8.x-1.5'
composer require 'drupal/message_notify:8.x-1.2'
Overview
Message Notify is an essential companion module for the Message module, providing a flexible and extensible notification framework. It enables sending Message entities to users through various delivery channels using a plugin-based architecture.
The module includes built-in support for email notifications and provides the foundation for SMS notifications (requires SMS Framework module). Developers can create custom notifier plugins to support additional delivery methods such as push notifications, Slack messages, or any other communication channel.
Message Notify leverages Drupal's view modes to control how message content is rendered for different notification channels. For email notifications, it automatically creates 'Notify - Email subject' and 'Notify - Email body' view modes, allowing site builders to configure which fields appear in email subjects and bodies separately.
Features
- Plugin-based notifier system allowing extensible delivery methods for sending messages
- Built-in Email notifier plugin that uses Drupal's mail system to deliver messages
- SMS notifier plugin foundation (requires SMS Framework module for actual delivery)
- Automatic creation of email view modes (mail_subject, mail_body) when new message templates are created
- Multilingual support with option to use message owner's preferred language or message's language
- Configurable message saving behavior on successful or failed delivery
- Ability to store rendered output in message fields for archival purposes
- Token support for dynamic content in messages (inherited from Message module)
- Customizable 'from' address for email notifications
- Access control for notifier plugins to determine if notifications should be sent
Use Cases
Comment Notification System
Send email notifications to content authors when comments are posted on their nodes. Create a message template with subject and body fields, configure the view modes, then use hook_comment_insert() to create and send a message to the node author using the message_notify.sender service.
User Registration Notifications
Notify administrators when new users register. Create a message template for registration notifications, then implement hook_user_insert() to create a message and send it to admin users via email.
Order Status Updates (E-commerce)
Send transactional emails for order status changes in e-commerce sites. Create message templates for each order status (placed, shipped, delivered) with appropriate content, then trigger notifications when order states change.
Content Moderation Workflow Notifications
Notify editors when content is submitted for review, or authors when content is published/rejected. Integrate with Drupal's Content Moderation to send appropriate notifications at each workflow transition.
Custom Multi-channel Notifications
Create a custom notifier plugin to send notifications through additional channels like Slack, Telegram, or push notifications. Extend MessageNotifierBase and implement the deliver() method to integrate with external services.
Digest Email System
Combine with Queue API to create digest emails. Instead of immediate delivery, queue messages and send periodic digest emails containing multiple notifications using custom view modes and templates.
Tips
- Configure the 'Notify - Email subject' view mode to display only the first partial of your message template text field to create concise email subjects.
- Use the 'language override' option to send emails in the message's language instead of the recipient's preferred language - useful for system notifications.
- Store rendered output in custom fields using the 'rendered fields' option to keep an archive of exactly what was sent to users.
- Create custom notifier plugins by extending MessageNotifierBase - you only need to implement the deliver() method.
- Use hook_mail_alter() to add custom headers, attachments, or modify emails before they are sent.
- The message entity is available in email $params['message_entity'] for use in hook_mail_alter() implementations.
- Test email delivery with modules like Maillog or Devel to capture outgoing emails during development.
- Enable the message_notify_example submodule to see a working implementation of comment notifications.
Technical Details
Hooks 3
hook_message_notifier_info_alter
Allows modules to alter notifier plugin definitions discovered by the plugin manager.
hook_mail
Message Notify implements hook_mail() to set email subject and body from the rendered message view modes.
hook_mail_alter
Standard Drupal hook that can be used to alter emails sent by Message Notify before delivery. The message entity is available in $params['message_entity'].
Troubleshooting 6
Verify that Drupal's mail system is properly configured. Check the mail system settings at /admin/config/system/mailsystem. Consider using a module like SMTP or Mailsystem to configure email delivery. Check the Drupal logs for any mail-related errors.
Ensure the view modes 'Notify - Email subject' and 'Notify - Email body' are properly configured for your message template. Go to Structure > Message templates > [Your template] > Manage Display and configure which fields appear in each view mode.
This error occurs when the specified notifier plugin doesn't exist. Verify the notifier name passed to send() method. Default is 'email'. For SMS, ensure SMS Framework module is installed and configured.
Messages must have an owner (uid) set, or you must pass a 'mail' option with the recipient's email address. Either set the message owner with $message->setOwnerId($uid) or pass ['mail' => 'recipient@example.com'] in the options array.
By default, messages are saved only on successful delivery. Check the 'save on success' and 'save on fail' options. Pass ['save on success' => TRUE, 'save on fail' => TRUE] to always save messages regardless of delivery result.
View modes are auto-created when new message bundles are created, but only if the module was enabled first. For existing templates, manually create entity view displays at Structure > Message templates > [Template] > Manage Display.
Security Notes 4
- Always validate and sanitize any user-provided content in message templates to prevent XSS attacks in email content.
- Be cautious with the 'mail' option - validate email addresses before sending to prevent email injection attacks.
- Email subjects automatically have HTML stripped for security, but body content should still be properly sanitized.
- Implement proper access controls in custom notifier plugins' access() method to prevent unauthorized notification sending.