You are here

class DevelMailLogger in Devel Mail Logger 8

Defines a mail backend that saves emails to DB.

To enable, save a variable in settings.php (or otherwise) whose value can be as simple as:

$config['system.mail']['interface']['default'] = 'devel_mail_logger';

Plugin annotation


@Mail(
  id = "devel_mail_logger",
  label = @Translation("Devel DB Mail Logger"),
  description = @Translation("Saves Mails to DB")
)

Hierarchy

Expanded class hierarchy of DevelMailLogger

File

src/Plugin/Mail/DevelMailLogger.php, line 26

Namespace

Drupal\devel_mail_logger\Plugin\Mail
View source
class DevelMailLogger implements MailInterface, ContainerFactoryPluginInterface {

  /**
   * Drupal\Core\Database\Driver\mysql\Connection definition.
   *
   * @var \Drupal\Core\Database\Driver\mysql\Connection
   */
  protected $database;

  /**
   * Constructs a new DevelMailLog object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Database\Connection $database
   *   A Database connection to use for reading and writing database data.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function mail(array $message) {
    $query = $this->database
      ->insert('devel_mail_logger')
      ->fields([
      'timestamp' => \Drupal::time()
        ->getRequestTime(),
      'recipient' => $message['to'],
      'subject' => $message['subject'],
      'message' => json_encode($message),
    ]);
    $query
      ->execute();
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function format(array $message) {

    // Join the body array into one string.
    $message['body'] = implode("\n\n", $message['body']);

    // Convert any HTML to plain-text.
    $message['body'] = MailFormatHelper::htmlToText($message['body']);

    // Wrap the mail body for sending.
    $message['body'] = MailFormatHelper::wrapMail($message['body']);
    return $message;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DevelMailLogger::$database protected property Drupal\Core\Database\Driver\mysql\Connection definition.
DevelMailLogger::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DevelMailLogger::format public function Formats a message prior to sending. Overrides MailInterface::format
DevelMailLogger::mail public function Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail(). Overrides MailInterface::mail
DevelMailLogger::__construct public function Constructs a new DevelMailLog object.