Commit 06039d15 authored by Larisse Amorim's avatar Larisse Amorim Committed by Damien McKenna
Browse files

Issue #3236380 by gabrielda, larisse, DamienMcKenna: Send email on success.

parent 1620587e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ By DamienMcKenna: Updated CHANGELOG.txt for recent 5.0.x changes already present
  to quick save tab.
#3256830 by hmendes, beatrizrodrigues, DamienMcKenna: Fix deprecation notices in
  tests.
#3236380 by gabrielda, larisse, DamienMcKenna: Send email on success.


Backup and Migrate 5.0.x-dev, xxxx-xx-xx
+12 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ use Drupal\backup_migrate\Drupal\Filter\DrupalUtils;
use Drupal\backup_migrate\Core\Filter\MetadataWriter;
use Drupal\backup_migrate\Core\File\TempFileManager;
use Drupal\backup_migrate\Drupal\File\DrupalTempFileAdapter;
use Drupal\backup_migrate\Core\Filter\Notify;

/**
 * Back up a source to 1 or more destinations.
@@ -165,6 +166,9 @@ function backup_migrate_backup_migrate_service_object_alter(BackupMigrateInterfa
    ])
  ));

  // Add the Notify email user
  $plugins->add('notify', new Notify());

  // Add the custom configured sources.
  foreach (Source::loadMultiple() as $source) {
    $source->getPlugin()->alterBackupMigrate($bam, $source->get('id'), $options);
@@ -329,3 +333,11 @@ function backup_migrate_module_version() {

  return $version;
}

/**
 * Implements hook_mail().
*/
function backup_migrate_mail($key, &$message, $params) {
  $message['subject'] = $key;
  $message['body'] = $params;
}
+2 −0
Original line number Diff line number Diff line
@@ -5,3 +5,5 @@ services:
  plugin.manager.backup_migrate_destination:
    class: Drupal\backup_migrate\Drupal\EntityPlugins\DestinationPluginManager
    parent: default_plugin_manager
  backup_migrate.mailer:
    class: Drupal\backup_migrate\Core\Service\Mailer
+48 −17
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\backup_migrate\Core\Plugin\PluginCallerInterface;
use Drupal\backup_migrate\Core\Plugin\PluginCallerTrait;
use Drupal\backup_migrate\Core\Service\StashLogger;
use Drupal\backup_migrate\Core\Service\TeeLogger;
use Drupal\backup_migrate\Core\File\BackupFileReadableInterface;

/**
 * Notifies by email when a backup succeeds or fails.
@@ -16,6 +17,39 @@ use Drupal\backup_migrate\Core\Service\TeeLogger;
class Notify extends PluginBase implements PluginCallerInterface {
  use PluginCallerTrait;

  /**
   * @var \Drupal\backup_migrate\Core\Service\StashLogger
   */
  protected $logstash;

  /**
   * {@inheritdoc}
   */
  public function configSchema(array $params = []) {
    $schema = [];

    // Backup configuration.
    if ($params['operation'] == 'backup') {
      $schema['groups']['notify'] = [
        'title' => 'Email Settings',
      ];
      $schema['fields']['notify_success_enable'] = [
        'group' => 'notify',
        'type' => 'boolean',
        'title' => 'Send an email if backup succeeds',
      ];
      $schema['fields']['notify_sucess_email'] = [
        'group' => 'notify',
        'type' => 'text',
        'title' => 'Email Address for Success Notices',
        'default_value' => \Drupal::config('system.site')->get('mail'),
        'description' => 'The email added to send a notification about backup.',
      ];
    }

    return $schema;
  }

  /**
   * Add a weight so that our before* operations run before any others.
   *
@@ -31,11 +65,6 @@ class Notify extends PluginBase implements PluginCallerInterface {
    ];
  }

  /**
   * @var \Drupal\backup_migrate\Core\Service\StashLogger
   */
  protected $logstash;

  /**
   *
   */
@@ -46,21 +75,27 @@ class Notify extends PluginBase implements PluginCallerInterface {
  /**
   *
   */
  public function beforeRestore() {
  public function beforeRestore(BackupFileReadableInterface $file) {
    $this->addLogger();
    return $file;
  }

  /**
   *
   * Call notification function if backup was succeed
   */
  public function backupSucceed() {
    $this->sendNotification('Backup finished sucessfully');
    if ($this->config->get('notify_success_enable')) {
      $subject = 'Backup finished successfully';
      $body = t('Site backup succeeded ' . \Drupal::config('system.site')->get('name'));
      $recipient = $this->config->get('notify_sucess_email');
      $this->sendNotification($subject, $body, $recipient);
    }
  }

  /**
   *
   */
  public function backupFail(Exception $e) {
  public function backupFail(\Exception $e) {

  }

@@ -79,14 +114,10 @@ class Notify extends PluginBase implements PluginCallerInterface {
  /**
   * @param $subject
   * @param $body
   * @param $messages
   * @param $recipient
   */
  protected function sendNotification($subject) {
    $messages = $this->logstash->getAll();
    $body = $subject . "\n";
    if (count($messages)) {

    }
  protected function sendNotification($subject, $body, $recipient) {
    \Drupal::service('backup_migrate.mailer')->send($recipient, $subject, $body);
  }

  /**
@@ -106,7 +137,7 @@ class Notify extends PluginBase implements PluginCallerInterface {

    // Add the services back into the plugin manager to re-inject existing
    // plugins.
    $this->plugins()->setServiceLocator($services);
    $this->plugins()->setServiceManager($services);
  }

  // @todo Add a tee to the logger to capture all messages.
+3 −3
Original line number Diff line number Diff line
@@ -27,8 +27,8 @@ class Mailer implements MailerInterface {
      $body = strtr($body, $replacements);
    }

    // Use the PHP mail function to send the message.
    mail($to, $subject, $body, $additional_headers);
    $langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
    \Drupal::service('plugin.manager.mail')->mail('backup_migrate', $subject, $to, $langcode, ['content' => $body]);
  }

}
 No newline at end of file
Loading