How to send notification email while adding a content in Drupal

During development of digital systems, communication with users of system is the one of the important part of the requirement of the system.

Here we are going to discuss how to send notifications to different users during activities like saving a node and publishing a node etc. With minimal code using contributed modules.

Here we are going to achieve this with swift mailer module and message notify module. Message notify module with parent message module allows us to manage message templates and other configuration at backend and also provides dedicated services for implementing our customized solutions.

First configure Swift Mailer module in you Drupal instance

See this Article for installation and configuration Swift Mailer module and confirm you are able to send test mails using your gmail SMTP server for example.

How to configure email in Drupal 9 using Swift mailer module.

Next install below module

Make sure you have below module installed before installing message notify. Use composer to install message module with message notify.

After installation and enabling above modules, Under configuration page you can see Mail system configuration.


Under module specific configurations, select below options.

And click Add button. You can see message notify in below table, click save configuration.

Now we have configured message notify module to send email using swift mailer module.

After installation you can see message templates under structure menu.

So in message template screen you can see the list of templates if you have created already, Also you can create templates for each purposes.

Here we are going to create new template called node notification.

Initially you can see only one message text box, so this we can use as the subject of the email. How to map as email subject and body, we are discussing later in this article.

Click on below Add another term button.

You can provide body text of the mail here, here as of now we are not using any dynamic values in template.

Save the template.

You can use multiple templates , and you can manage which message text is the Email subject and which one is the mail body as below.


In below Manage display tab , select Notify – Email -Body/ Notify Email Subject tabs. You can see partial 0, partial 1, partial 2 .

Partials are the message text fields shown below. Based on order, starts from 0 . you can decide which partial is mail subject and which one is mail body. Disable partials which is no needed in each section.

Next we need to send this template while saving node, say while creating a Article content we need to send above notification to author of the node.

So for this we need to provide below code in hook_node_presave of our custom module .module file.

function dn_notification_node_presave(\Drupal\Core\Entity\EntityInterface $node) {
  // Call message notify service.
  $notifier = \Drupal::service('message_notify.sender');
  // Create a message with node author as message creator.
  $message = Message::create(['template' => 'node_notification', 'uid' => $node->getOwnerId()]);
  //$message->set('field_text_message', 'text message'); // this is for passing value later we can uncomment
  // Send message to message creator(in this case = node author).


dn_notification is the name of custom module we are using.

Clear the cache

Add a content in Article. In node author mail id below mail will receive in inbox.

Passing values to the message template

Here we are passing a dynamic value to message template. For this first create a filed in your  message template.

In your template edit page you can see browse token link as below just after Message text box.

In your message text box , click on text area where you wan to appear passing value, then click on above token link. Then click on messages in token list as below.

In same list you can see your field,

By clicking on that field_text_message token it will be added to message text box as below.

Next save message template

So in node_presave hook un comment below line

$message->set(‘field_text_message’, ‘text message’);

So while saving content text message string will be passed to the message template.

In same you can add multiple tokens to the message text.


You can add isPublished condition in node_presave if you want to send notification on if node published.

	  $notifier = \Drupal::service('message_notify.sender');
	  // Create a message with node author as message creator.
	  $message = Message::create(['template' => 'node_notification', 'uid' => $node->getOwnerId()]);
	  $message->set('field_text_message', 'text message');
	  // Send message to message creator(in this case = node author).

Download sample dn_notification module here 

Note : If you are enabling message example module, there is a chance of getting below error. it is because of some value set wrongly in sample data. you will get below error.

Notice: Undefined index: format in Drupal\message\Entity\MessageTemplate->getText()

reason is there in below link

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...