Automating Post-Migration Emails with Drupal Migration Suite
Lakshman Kumar Pandey
2+ Exits | 12+ years in Tech | Drupal & BigCommerce Specialist | AI-Enhanced Migrations & Integrations
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:
Prerequisites
Before you start, ensure you have:
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!