You are here

protected function SwiftMailer::embed in Swift Mailer 8.2

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

Process inline images..

@internal

Parameters

\Swift_Message $m: The message which inline images are to be added to.

array $images: The images which are to be added as inline images to the provided message.

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

File

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

Class

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

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

protected function embed(Swift_Message $m, array $images) {

  // Iterate through each array element.
  foreach ($images as $image) {
    if ($image instanceof stdClass) {

      // Validate required fields.
      if (empty($image->uri) || empty($image->filename) || empty($image->filemime) || empty($image->cid)) {
        continue;
      }

      // Keep track of the 'cid' assigned to the embedded image.
      $cid = NULL;

      // Get image data.
      if (UrlHelper::isValid($image->uri, TRUE)) {
        $content = file_get_contents($image->uri);
      }
      else {
        $content = file_get_contents(\Drupal::service('file_system')
          ->realpath($image->uri));
      }
      $filename = $image->filename;
      $filemime = $image->filemime;

      // Embed image.
      $cid = $m
        ->embed(new Swift_Image($content, $filename, $filemime));

      // The provided 'cid' needs to be replaced with the 'cid' returned
      // by the Swift Mailer library.
      $body = $m
        ->getBody();
      $body = preg_replace('/cid:' . $image->cid . '/', $cid, $body);
      $m
        ->setBody($body);
    }
  }
}