You are here

class SocialSwiftMailer in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  2. 8 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  3. 8.2 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  4. 8.3 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  5. 8.4 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  6. 8.5 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  7. 8.6 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  8. 8.7 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  9. 8.8 modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  10. 10.3.x modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  11. 10.1.x modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer
  12. 10.2.x modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer

Provides a 'Forced HTML SwiftMailer' plugin to send emails.

Plugin annotation


@Mail(
  id = "social_swiftmailer",
  label = @Translation("Social Swift Mailer"),
  description = @Translation("Forces the given body text to be HTML.")
)

Hierarchy

  • class \Drupal\social_swiftmail\Plugin\Mail\SocialSwiftMailer extends \Drupal\swiftmailer\Plugin\Mail\SwiftMailer

Expanded class hierarchy of SocialSwiftMailer

File

modules/social_features/social_swiftmail/src/Plugin/Mail/SocialSwiftMailer.php, line 22

Namespace

Drupal\social_swiftmail\Plugin\Mail
View source
class SocialSwiftMailer extends SwiftMailer {

  /**
   * {@inheritdoc}
   */
  protected function massageMessageBody(array &$message, $is_html) {
    $line_endings = Settings::get('mail_line_endings', PHP_EOL);

    // Overwrite the Markup::Create of SwiftMailer.php.
    // This to take in to account our tables / <br> and basically
    // the custom HTML that is added within open social.
    $message['body'] = Markup::create(implode($line_endings, array_map(function ($body) {

      // If the field contains no html tags we can assume newlines will need be
      // converted to <br>.
      if (strlen(strip_tags($body)) === strlen($body)) {
        $body = str_replace("\r", '', $body);
        $body = str_replace("\n", '<br>', $body);
      }
      return check_markup($body, 'full_html');
    }, $message['body'])));

    // @see: SwiftMailer::massageMessageBody()
    // Attempt to use the mail theme defined in MailSystem.
    if ($this->mailManager instanceof MailsystemManager) {
      $mail_theme = $this->mailManager
        ->getMailTheme();
    }
    else {
      $mail_theme = $this->themeManager
        ->getActiveTheme()
        ->getName();
    }
    $render = [
      '#theme' => $message['params']['theme'] ?? 'swiftmailer',
      '#message' => $message,
      '#is_html' => $is_html,
    ];
    if ($is_html) {
      $render['#attached']['library'] = [
        "{$mail_theme}/swiftmailer",
      ];
    }
    $message['body'] = $this->renderer
      ->renderPlain($render);
    if ($is_html) {

      // Process CSS from libraries.
      $assets = AttachedAssets::createFromRenderArray($render);
      $css = '';

      // Request optimization so that the CssOptimizer performs essential
      // processing such as @include.
      foreach ($this->assetResolver
        ->getCssAssets($assets, TRUE) as $css_asset) {
        $css .= file_get_contents($css_asset['data']);
      }
      if ($css) {
        $message['body'] = (new CssToInlineStyles())
          ->convert($message['body'], $css);
      }
    }
    else {

      // Convert to plain text.
      $message['body'] = (new Html2Text($message['body']))
        ->getText();
    }
  }

}

Members