You are here

private function SwiftMailer::embed in Swift Mailer 8

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

Process inline images..

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 409

Class

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

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

private 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(Swift_Image::newInstance($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);
    }
  }
}