MailManager.php in Easy Email 8
File
modules/easy_email_override/src/Service/MailManager.php
View source
<?php
namespace Drupal\easy_email_override\Service;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Traversable;
class MailManager extends DefaultPluginManager implements MailManagerInterface {
protected $decorated;
public function __construct(MailManagerInterface $decorated, \Traversable $namespaces, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Mail', $namespaces, $module_handler, 'Drupal\\Core\\Mail\\MailInterface', 'Drupal\\Core\\Annotation\\Mail');
$this->decorated = $decorated;
}
public function getInstance(array $options) {
return $this->decorated
->getInstance($options);
}
public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
$email_handler = \Drupal::service('easy_email.handler');
$email_overrides = \Drupal::entityTypeManager()
->getStorage('easy_email_override')
->loadByProperties([
'module' => $module,
'key' => $key,
]);
if (!empty($email_overrides)) {
foreach ($email_overrides as $email_override) {
$email = $email_handler
->createEmail([
'type' => $email_override
->getEasyEmailType(),
]);
$param_map = $email_override
->getParamMap();
foreach ($param_map as $pm) {
$email
->set($pm['destination'], $params[$pm['source']]);
}
$result = $email_handler
->sendEmail($email);
$send = FALSE;
}
}
$message = $this->decorated
->mail($module, $key, $to, $langcode, $params, $reply, $send);
if (!isset($message['result']) && !empty($email_overrides)) {
$message['result'] = TRUE;
}
return $message;
}
}