EmailAttachmentEvaluator.php in Easy Email 2.0.x
File
src/Service/EmailAttachmentEvaluator.php
View source
<?php
namespace Drupal\easy_email\Service;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser;
use Drupal\easy_email\Entity\EasyEmailInterface;
use Drupal\easy_email\Event\EasyEmailEvent;
use Drupal\easy_email\Event\EasyEmailEvents;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class EmailAttachmentEvaluator implements EmailAttachmentEvaluatorInterface {
protected $fileSystem;
protected $mimeTypeGuesser;
protected $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher, FileSystemInterface $fileSystem, MimeTypeGuesser $mimeTypeGuesser) {
$this->fileSystem = $fileSystem;
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->eventDispatcher = $eventDispatcher;
}
public function evaluateAttachments(EasyEmailInterface $email, $save_attachments_to = FALSE) {
$this->eventDispatcher
->dispatch(EasyEmailEvents::EMAIL_PREATTACHMENTEVAL, new EasyEmailEvent($email));
$files = $email
->getEvaluatedAttachments();
if (!empty($save_attachments_to) && !empty($files)) {
foreach ($files as $i => $file) {
$this
->saveAttachment($email, $file->uri, $save_attachments_to);
unset($files[$i]);
}
}
if ($email
->hasField('attachment')) {
$attachments = $email
->getAttachments();
if (!empty($attachments)) {
foreach ($attachments as $attachment) {
$file = new \stdClass();
$file->uri = $attachment
->getFileUri();
$file->filename = $attachment
->getFilename();
$file->filemime = $attachment
->getMimeType();
$files[] = $file;
}
}
}
if ($email
->hasField('attachment_path')) {
$attachment_paths = $email
->getAttachmentPaths();
if (!empty($attachment_paths)) {
foreach ($attachment_paths as $path) {
if (strpos($path, '/') === 0) {
$path = substr($path, 1);
}
$realpath = $this->fileSystem
->realpath($path);
if (!file_exists($realpath)) {
continue;
}
if (!empty($save_attachments_to) && $email
->hasField('attachment')) {
$this
->saveAttachment($email, $realpath, $save_attachments_to);
}
$file = new \stdClass();
$file->uri = $path;
$file->filename = $this->fileSystem
->basename($path);
$file->filemime = $this->mimeTypeGuesser
->guess($path);
$files[] = $file;
}
}
}
$email
->setEvaluatedAttachments($files);
$this->eventDispatcher
->dispatch(EasyEmailEvents::EMAIL_ATTACHMENTEVAL, new EasyEmailEvent($email));
}
protected function saveAttachment(EasyEmailInterface $email, $source, $dest_directory) {
\Drupal::service('file_system')
->prepareDirectory($dest_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$file_entity = file_save_data(file_get_contents($source), $dest_directory . '/' . $this->fileSystem
->basename($source));
$email
->addAttachment($file_entity
->id());
}
}