I'm currently dealing with a client who is using a transactional mail service which has it's own MailSystem class. The client just sent out lots of emails which has exhausted their email quota. So for critical emails like "password resets" I would still like Drupal to handle the sending of those emails. Is it possible to detect which module/key the email is being sent from (e.g. user/password_reset) and choose an alternate MailSystem?

Comments

hargobind created an issue. See original summary.

hargobind’s picture

Component: Code » Documentation
Issue summary: View changes
Status: Active » Fixed

I found the answer to my own question.

In my case, I had created a simple sandbox module to load my generated MailSystem classes. It uses the HTMLMail module for formatting, and MailJet to send emails. During the send process, I modified the code to check the module value of the message and see if it belongs to the user module, then send it via Drupal's DefaultMailSystem. Other useful keys in $message are 'module' and 'key', and 'id' which is a concatenation of "module_key".

<?php
class HTMLMailSystem__MailjetSmtpMailSystem implements MailSystemInterface {
  protected $formatClass;
  protected $mailClass;
  public function __construct() {
    if (drupal_autoload_class('HTMLMailSystem')) {
      $this->formatClass = new HTMLMailSystem;
    }
    else {
      $this->formatClass = new DefaultMailSystem;
    }
    if (drupal_autoload_class('MailjetSmtpMailSystem')) {
      $this->mailClass = new MailjetSmtpMailSystem;
    }
    else {
      $this->mailClass = new DefaultMailSystem;
    }
  }
  public function format(array $message) {
    return $this->formatClass->format($message);
  }
  public function mail(array $message) {
    // Send critical emails from Drupal.
    if ($message['module'] == 'user') {
      $this->mailClass = new DefaultMailSystem;
    }
    return $this->mailClass->mail($message);
  }
}

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.