You are here

protected function SwiftMailer::attach in Swift Mailer 8.2

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

Process attachments.

@internal

Parameters

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

array $files: The files which are to be added as attachments to the provided message.

2 calls to SwiftMailer::attach()
SwiftMailer::attachAsMimeMail in src/Plugin/Mail/SwiftMailer.php
Process MimeMail attachments.
SwiftMailer::mail in src/Plugin/Mail/SwiftMailer.php
Sends a message composed by drupal_mail().

File

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

Class

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

Namespace

Drupal\swiftmailer\Plugin\Mail

Code

protected function attach(Swift_Message $m, array $files) {

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

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

      // Get file data from local file, stream, or remote (e.g. http(s)) uri.
      $content = file_get_contents($file->uri);
      $filename = $file->filename;
      $filemime = $file->filemime;

      // Attach file.
      $m
        ->attach(new Swift_Attachment($content, $filename, $filemime));
    }
  }
}