You are here

public static function Conversion::swiftmailer_filter_message in Swift Mailer 8

Same name and namespace in other branches
  1. 8.2 src/Utility/Conversion.php \Drupal\swiftmailer\Utility\Conversion::swiftmailer_filter_message()

Filters out unwanted elements from a message.

Parameters

\Swift_Message $message: The message which unwanted elements is to be filtered out from.

1 call to Conversion::swiftmailer_filter_message()
SwiftMailer::mail in src/Plugin/Mail/SwiftMailer.php
Sends a message composed by drupal_mail().

File

src/Utility/Conversion.php, line 410

Class

Conversion
@todo

Namespace

Drupal\swiftmailer\Utility

Code

public static function swiftmailer_filter_message(Swift_Message $message) {
  $headers = $message
    ->getHeaders();
  $senders = $headers
    ->get('From')
    ->getAddresses();
  if (!empty($senders)) {
    for ($i = 0; $i < count($senders); $i++) {
      if (!\Drupal::service('email.validator')
        ->isValid($senders[$i])) {
        $headers
          ->remove('From', $i);
        \Drupal::logger('swiftmailer')
          ->warning('The invalid "From" e-mail address "@mail" was skipped.', [
          '@mail' => $senders[$i],
        ]);
      }
    }
  }
  $recipients = $headers
    ->get('To')
    ->getAddresses();
  if (!empty($recipients)) {
    for ($i = 0; $i < count($recipients); $i++) {
      if (!\Drupal::service('email.validator')
        ->isValid($recipients[$i])) {
        $headers
          ->remove('To', $i);
        \Drupal::logger('swiftmailer')
          ->warning('The invalid "To" e-mail address "@mail" was skipped.', [
          '@mail' => $recipients[$i],
        ]);
      }
    }
  }
}