You are here

public function Maillog::mail in Maillog / Mail Developer 8

Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().

Parameters

array $message: Message array with at least the following elements:

  • id: A unique identifier of the email type. Examples: 'contact_user_copy', 'user_password_reset'.
  • to: The mail address or addresses where the message will be sent to. The formatting of this string will be validated with the PHP email validation filter. Some examples:

  • subject: Subject of the email to be sent. This must not contain any newline characters, or the mail may not be sent properly. The subject is converted to plain text by the mail plugin manager.
  • body: Message to be sent. Accepts both CRLF and LF line-endings. Email bodies must be wrapped. For smart plain text wrapping you can use \Drupal\Core\Mail\MailFormatHelper::wrapMail() .
  • headers: Associative array containing all additional mail headers not defined by one of the other parameters. PHP's mail() looks for Cc and Bcc headers and sends the mail to addresses in these headers too.

Return value

bool TRUE if the mail was successfully accepted for delivery, otherwise FALSE.

Overrides MailInterface::mail

File

src/Plugin/Mail/Maillog.php, line 36

Class

Maillog
Provides a 'Dummy' plugin to send emails.

Namespace

Drupal\maillog\Plugin\Mail

Code

public function mail(array $message) {
  $config = \Drupal::configFactory()
    ->get('maillog.settings');

  // Log the e-mail.
  if ($config
    ->get('log')) {
    $record = new \stdClass();

    // In case the subject/from/to is already encoded, decode with
    // Unicode::mimeHeaderDecode().
    $record->header_message_id = isset($message['headers']['Message-ID']) ? $message['headers']['Message-ID'] : $this
      ->t('Not delivered');
    $record->subject = $message['subject'];
    $record->subject = mb_substr(Unicode::mimeHeaderDecode($record->subject), 0, 255);
    $record->body = $message['body'];
    $record->header_from = isset($message['from']) ? $message['from'] : NULL;
    $record->header_from = Unicode::mimeHeaderDecode($record->header_from);
    $header_to = [];
    if (isset($message['to'])) {
      if (is_array($message['to'])) {
        foreach ($message['to'] as $value) {
          $header_to[] = Unicode::mimeHeaderDecode($value);
        }
      }
      else {
        $header_to[] = Unicode::mimeHeaderDecode($message['to']);
      }
    }
    $record->header_to = implode(', ', $header_to);
    $record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
    $record->header_all = serialize($message['headers']);
    $record->sent_date = \Drupal::time()
      ->getRequestTime();
    Database::getConnection()
      ->insert('maillog')
      ->fields((array) $record)
      ->execute();
  }

  // Display the email if the verbose is enabled.
  if ($config
    ->get('verbose') && \Drupal::currentUser()
    ->hasPermission('view maillog')) {

    // Print the message.
    $header_output = print_r($message['headers'], TRUE);
    $output = $this
      ->t('A mail has been sent: <br/> [Subject] => @subject <br/> [From] => @from <br/> [To] => @to <br/> [Reply-To] => @reply <br/> <pre>  [Header] => @header <br/> [Body] => @body </pre>', [
      '@subject' => $message['subject'],
      '@from' => $message['from'],
      '@to' => $message['to'],
      '@reply' => isset($message['reply_to']) ? $message['reply_to'] : NULL,
      '@header' => $header_output,
      '@body' => $message['body'],
    ]);
    \Drupal::messenger()
      ->addStatus($output, TRUE);
  }
  if ($config
    ->get('send')) {
    $default = new PhpMail();
    $result = $default
      ->mail($message);
  }
  elseif (\Drupal::currentUser()
    ->hasPermission('administer maillog')) {
    $message = $this
      ->t('Sending of e-mail messages is disabled by Maillog module. Go @here to enable.', [
      '@here' => \Drupal::service('link_generator')
        ->generate('here', Url::fromRoute('maillog.settings')),
    ]);
    \Drupal::messenger()
      ->addWarning($message, TRUE);
  }
  else {
    \Drupal::logger('maillog')
      ->notice('Attempted to send an email, but sending emails is disabled.');
  }
  return isset($result) ? $result : TRUE;
}