You are here

public function WebformCompositeBase::getAttachments in Webform 8.5

Get email attachments.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

array An array containing email attachments which include an attachments 'filename', 'filemime', 'filepath', and 'filecontent'.

Overrides WebformElementAttachmentInterface::getAttachments

See also

\Drupal\mimemail\Utility\MimeMailFormatHelper::mimeMailHtmlBody

\Drupal\smtp\Plugin\Mail\SMTPMailSystem::mail

\Drupal\swiftmailer\Plugin\Mail\SwiftMailer::attachAsMimeMail

File

src/Plugin/WebformElement/WebformCompositeBase.php, line 1478

Class

WebformCompositeBase
Provides a base for composite elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function getAttachments(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $data = $webform_submission
    ->getData();

  // Get file ids.
  $fids = [];
  $composite_elements_managed_files = $this
    ->getManagedFiles($element);
  foreach ($composite_elements_managed_files as $composite_key) {
    $fids = array_merge($fids, $this
      ->getManagedFileIdsFromData($element, $data, $composite_key));
  }
  if (!$fids) {
    return [];
  }

  // Get attachments.
  $attachments = [];
  $files = File::loadMultiple($fids);
  foreach ($files as $file) {
    $attachments[] = [
      'filecontent' => file_get_contents($file
        ->getFileUri()),
      'filename' => $file
        ->getFilename(),
      'filemime' => $file
        ->getMimeType(),
      // File URIs that are not supported return FALSE, when this happens
      // still use the file's URI as the file's path.
      'filepath' => \Drupal::service('file_system')
        ->realpath($file
        ->getFileUri()) ?: $file
        ->getFileUri(),
      // URI is used when debugging or resending messages.
      // @see \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::buildAttachments
      '_fileurl' => file_create_url($file
        ->getFileUri()),
    ];
  }
  return $attachments;
}