You are here

public function SwiftMailer::mail in Swift Mailer 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Mail/SwiftMailer.php \Drupal\swiftmailer\Plugin\Mail\SwiftMailer::mail()

Sends a message composed by drupal_mail().

Parameters

array $message: A message array holding all relevant details for the message.

Return value

bool TRUE if the message was successfully sent, and otherwise FALSE.

Overrides MailInterface::mail

See also

http://api.drupal.org/api/drupal/includes--mail.inc/interface/MailSystem...

File

src/Plugin/Mail/SwiftMailer.php, line 185

Class

SwiftMailer
Provides a 'Swift Mailer' plugin to send emails.

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

public function mail(array $message) {
  try {

    // Create a new message.
    $m = Swift_Message::newInstance($message['subject']);

    // Not all Drupal headers should be added to the e-mail message.
    // Some headers must be suppressed in order for Swift Mailer to
    // do its work properly.
    $suppressable_headers = swiftmailer_get_supressable_headers();

    // Keep track of whether we need to respect the provided e-mail
    // format or not.
    $respect_format = $this->config['message']['respect_format'];

    // Process headers provided by Drupal. We want to add all headers which
    // are provided by Drupal to be added to the message. For each header we
    // first have to find out what type of header it is, and then add it to
    // the message as the particular header type.
    if (!empty($message['headers']) && is_array($message['headers'])) {
      foreach ($message['headers'] as $header_key => $header_value) {

        // Check wether the current header key is empty or represents
        // a header that should be suppressed. If yes, then skip header.
        if (empty($header_key) || in_array($header_key, $suppressable_headers)) {
          continue;
        }

        // Skip 'Content-Type' header if the message to be sent will be a
        // multipart message or the provided format is not to be respected.
        if ($header_key == 'Content-Type' && (!$respect_format || swiftmailer_is_multipart($message))) {
          continue;
        }

        // Get header type.
        $header_type = Conversion::swiftmailer_get_headertype($header_key, $header_value);

        // Add the current header to the e-mail message.
        switch ($header_type) {
          case SWIFTMAILER_HEADER_ID:
            Conversion::swiftmailer_add_id_header($m, $header_key, $header_value);
            break;
          case SWIFTMAILER_HEADER_PATH:
            Conversion::swiftmailer_add_path_header($m, $header_key, $header_value);
            break;
          case SWIFTMAILER_HEADER_MAILBOX:
            Conversion::swiftmailer_add_mailbox_header($m, $header_key, $header_value);
            break;
          case SWIFTMAILER_HEADER_DATE:
            Conversion::swiftmailer_add_date_header($m, $header_key, $header_value);
            break;
          case SWIFTMAILER_HEADER_PARAMETERIZED:
            Conversion::swiftmailer_add_parameterized_header($m, $header_key, $header_value);
            break;
          default:
            Conversion::swiftmailer_add_text_header($m, $header_key, $header_value);
            break;
        }
      }
    }

    // \Drupal\Core\Mail\Plugin\Mail\PhpMail respects $message['to'] but for
    // 'from' and 'reply-to' it uses the headers (which are set in
    // MailManager::doMail). Replicate that behavior here.
    Conversion::swiftmailer_add_mailbox_header($m, 'To', $message['to']);

    // Get applicable format.
    $applicable_format = $this
      ->getApplicableFormat($message);

    // Get applicable character set.
    $applicable_charset = $this
      ->getApplicableCharset($message);

    // Set body.
    $m
      ->setBody($message['body'], $applicable_format, $applicable_charset);

    // Add alternative plain text version if format is HTML and plain text
    // version is available.
    if ($applicable_format == SWIFTMAILER_FORMAT_HTML && !empty($message['plain'])) {
      $m
        ->addPart($message['plain'], SWIFTMAILER_FORMAT_PLAIN, $applicable_charset);
    }

    // Validate that $message['params']['files'] is an array.
    if (empty($message['params']['files']) || !is_array($message['params']['files'])) {
      $message['params']['files'] = [];
    }

    // Let other modules get the chance to add attachable files.
    $files = $this->moduleHandler
      ->invokeAll('swiftmailer_attach', [
      'key' => $message['key'],
      'message' => $message,
    ]);
    if (!empty($files) && is_array($files)) {
      $message['params']['files'] = array_merge(array_values($message['params']['files']), array_values($files));
    }

    // Attach files.
    if (!empty($message['params']['files']) && is_array($message['params']['files'])) {
      $this
        ->attach($m, $message['params']['files']);
    }

    // Attach files (provide compatibility with mimemail)
    if (!empty($message['params']['attachments']) && is_array($message['params']['attachments'])) {
      $this
        ->attachAsMimeMail($m, $message['params']['attachments']);
    }

    // Embed images.
    if (!empty($message['params']['images']) && is_array($message['params']['images'])) {
      $this
        ->embed($m, $message['params']['images']);
    }

    // Get the configured transport type.
    $transport_type = $this->transportFactory
      ->getDefaultTransportMethod();
    $transport = $this->transportFactory
      ->getTransport($transport_type);
    $mailer = Swift_Mailer::newInstance($transport);

    // Allows other modules to customize the message.
    $this->moduleHandler
      ->alter('swiftmailer', $mailer, $m, $message);

    // Send the message.
    Conversion::swiftmailer_filter_message($m);

    /** @var Swift_Mailer $mailer */
    return (bool) $mailer
      ->send($m);
  } catch (Exception $e) {
    $headers = !empty($m) ? $m
      ->getHeaders() : '';
    $headers = !empty($headers) ? nl2br($headers
      ->toString()) : 'No headers were found.';
    $this->logger
      ->error('An attempt to send an e-mail message failed, and the following error
        message was returned : @exception_message<br /><br />The e-mail carried
        the following headers:<br /><br />@headers', [
      '@exception_message' => $e
        ->getMessage(),
      '@headers' => $headers,
    ]);
    \Drupal::messenger()
      ->addError(t('An attempt to send an e-mail message failed.'));
  }
  return FALSE;
}