You are here

public function Mailer::sendMail in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::sendMail()
  2. 8 src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::sendMail()

Send a node to an email address.

Parameters

\Drupal\simplenews\Mail\MailInterface $mail: The mail object.

Return value

int One of the SpoolStorageInterface::STATUS_* constants.

Overrides MailerInterface::sendMail

2 calls to Mailer::sendMail()
Mailer::sendSpool in src/Mail/Mailer.php
Send simplenews newsletters from the spool.
Mailer::sendTest in src/Mail/Mailer.php
Send test version of newsletter.

File

src/Mail/Mailer.php, line 323

Class

Mailer
Default Mailer.

Namespace

Drupal\simplenews\Mail

Code

public function sendMail(MailInterface $mail) {
  $params['simplenews_mail'] = $mail;
  if ($mail
    ->getKey('test') == 'node') {

    // Suppress error message as it causes cron failures.
    $params['_error_message'] = FALSE;
  }

  // Send mail.
  try {
    $message = $this->mailManager
      ->mail('simplenews', $mail
      ->getKey(), $mail
      ->getRecipient(), $mail
      ->getLanguage(), $params, $mail
      ->getFromFormatted());

    // Log sent result in watchdog.
    if ($this->config
      ->get('mail.debug')) {
      if ($message['result']) {
        $this->logger
          ->debug('Outgoing email. Message type: %type<br />Subject: %subject<br />Recipient: %to', [
          '%type' => $mail
            ->getKey(),
          '%to' => $message['to'],
          '%subject' => $message['subject'],
        ]);
      }
      else {
        $this->logger
          ->error('Outgoing email failed. Message type: %type<br />Subject: %subject<br />Recipient: %to', [
          '%type' => $mail
            ->getKey(),
          '%to' => $message['to'],
          '%subject' => $message['subject'],
        ]);
      }
    }

    // By default, failures are left in PENDING state to retry.
    $result = $message['result'] ? SpoolStorageInterface::STATUS_DONE : SpoolStorageInterface::STATUS_PENDING;
    $this->moduleHandler
      ->alter('simplenews_mail_result', $result, $message);
  } catch (SkipMailException $e) {
    $result = SpoolStorageInterface::STATUS_SKIPPED;
  }
  return $result;
}