Automating Post-Migration Emails with Drupal Migration Suite

Automating Post-Migration Emails with Drupal Migration Suite

Migrating data into Drupal can be a complex task, but the Drupal Migration Suite simplifies this process significantly. Once the migration is complete, it is often necessary to notify relevant stakeholders via email. Automating this notification process ensures timely communication and frees you from manual follow-up tasks. In this article, we'll explore how to send an email after a migration completes using the Drupal Migration Suite, with a detailed code example.

Why Automate Post-Migration Emails?

Automating post-migration emails offers several benefits:

  • Efficiency: Saves time by eliminating the need for manual email sending.
  • Consistency: Ensures all stakeholders receive notifications without oversight.
  • Accountability: Provides a clear record of migration completion.

Prerequisites

Before you start, ensure you have:

  1. Drupal 9/10 installed.
  2. Migrate Plus and Migrate Tools modules enabled.
  3. SwiftMailer or SMTP module for sending emails configured.

Step-by-Step Guide

1. Create a Custom Module

First, create a custom module. Let's name it migration_notify.

Directory structure:

modules/custom/migration_notify/
  - migration_notify.info.yml
  - migration_notify.module
  - src/EventSubscriber/MigrationCompleteSubscriber.php
        


migration_notify.info.yml:

name: 'Migration Notify'
type: module
description: 'Sends an email notification after migration completes.'
package: Custom
core_version_requirement: ^9 || ^10
dependencies:
  - drupal:migrate_tools        


2. Implement the Event Subscriber

Create the event subscriber that will listen for the migration complete event.

src/EventSubscriber/MigrationCompleteSubscriber.php:

<?php

namespace Drupal\migration_notify\EventSubscriber;

use Drupal\Core\Mail\MailManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class MigrationCompleteSubscriber.
 */
class MigrationCompleteSubscriber implements EventSubscriberInterface {

  /**
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * Constructs a MigrationCompleteSubscriber object.
   *
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager service.
   */
  public function __construct(MailManagerInterface $mail_manager) {
    $this->mailManager = $mail_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[MigrateEvents::POST_ROW_SAVE][] = ['onMigratePostRowSave'];
    return $events;
  }

  /**
   * Sends an email when the migration is complete.
   *
   * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
   *   The migrate post row save event.
   */
  public function onMigratePostRowSave(MigratePostRowSaveEvent $event) {
    $migration_id = $event->getMigration()->id();
    $to = '[email protected]'; // Replace with the actual recipient email
    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $params = [
      'subject' => t('Migration Complete: @migration', ['@migration' => $migration_id], ['langcode' => $langcode]),
      'message' => t('The migration @migration has been completed successfully.', ['@migration' => $migration_id], ['langcode' => $langcode]),
    ];

    $this->mailManager->mail('migration_notify', 'migration_complete', $to, $langcode, $params);
  }
}        


3. Implement the Hook Mail

migration_notify.module:

<?php

use Drupal\Core\Mail\MailManagerInterface;

/**
 * Implements hook_mail().
 */
function migration_notify_mail($key, &$message, $params) {
  switch ($key) {
    case 'migration_complete':
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['message'];
      break;
  }
}        


4. Register the Service

migration_notify.services.yml:

services:
  migration_notify.migration_complete_subscriber:
    class: Drupal\migration_notify\EventSubscriber\MigrationCompleteSubscriber
    arguments: ['@plugin.manager.mail']
    tags:
      - { name: event_subscriber }        


Conclusion

By following these steps, you can automate email notifications upon migration completion in Drupal. This approach leverages Drupal's event system to listen for migration completion events and sends an email using Drupal's mail API. This automation ensures timely and consistent communication with stakeholders, enhancing the overall efficiency of your migration projects.

Feel free to customize the email content and recipient list as per your project requirements. Happy migrating!

要查看或添加评论,请登录

Lakshman Kumar Pandey的更多文章

社区洞察

其他会员也浏览了