You are here

public function WebformImageFile::getEmailAttachments in Webform 6.x

Get files as email attachments.

This is also used to export 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 WebformManagedFileBase::getEmailAttachments

See also

\Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::getMessageAttachments

\Drupal\mimemail\Utility\MimeMailFormatHelper::mimeMailHtmlBody

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

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

File

src/Plugin/WebformElement/WebformImageFile.php, line 154

Class

WebformImageFile
Provides a 'webform_image_file' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function getEmailAttachments(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $attachments = [];

  /** @var \Drupal\image\ImageStyleInterface $image_style */
  $image_style = NULL;
  $attachment_image_style = $this
    ->getElementProperty($element, 'attachment_image_style');
  if ($attachment_image_style && $this->moduleHandler
    ->moduleExists('image')) {
    $image_style = $this->entityTypeManager
      ->getStorage('image_style')
      ->load($attachment_image_style);
  }
  $files = $this
    ->getTargetEntities($element, $webform_submission, $options) ?: [];
  foreach ($files as $file) {
    if ($image_style) {
      $file_uri = $image_style
        ->buildUri($file
        ->getFileUri());
      if (!file_exists($file_uri)) {
        $image_style
          ->createDerivative($file
          ->getFileUri(), $file_uri);
      }
      $file_url = $image_style
        ->buildUrl($file
        ->getFileUri());
    }
    else {
      $file_uri = $file
        ->getFileUri();
      $file_url = file_create_url($file
        ->getFileUri());
    }
    $attachments[] = [
      'filecontent' => file_get_contents($file_uri),
      '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' => $this->fileSystem
        ->realpath($file
        ->getFileUri()) ?: $file
        ->getFileUri(),
      // URL is used when debugging or resending messages.
      // @see \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::buildAttachments
      '_fileurl' => $file_url,
    ];
  }
  return $attachments;
}