You are here

protected function SparkpostMail::getAttachmentStruct in Sparkpost email 8.2

Return an array structure for a message attachment.

Parameters

string $path: Attachment path.

Return value

array Attachment structure.

Throws

\Exception

1 call to SparkpostMail::getAttachmentStruct()
SparkpostMail::mail in src/Plugin/Mail/SparkpostMail.php
Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().

File

src/Plugin/Mail/SparkpostMail.php, line 159

Class

SparkpostMail
Sparkpost mail plugin.

Namespace

Drupal\sparkpost\Plugin\Mail

Code

protected function getAttachmentStruct($path) {
  $struct = [];
  if (!@is_file($path)) {
    throw new \Exception($path . ' is not a valid file.');
  }
  $filename = basename($path);
  $file_buffer = file_get_contents($path);
  $file_buffer = chunk_split(base64_encode($file_buffer), 76, "\n");
  $mime_type = \Drupal::service('file.mime_type.guesser')
    ->guess($path);
  if (!$this
    ->isValidContentType($mime_type)) {
    throw new \Exception($mime_type . ' is not a valid content type.');
  }
  $struct['type'] = $mime_type;
  $struct['name'] = $filename;
  $struct['data'] = $file_buffer;
  return $struct;
}