Skip to content

Commit

Permalink
Merge pull request #91 from ger4003/feature-multipart-email-finisher
Browse files Browse the repository at this point in the history
FEATURE: send multipart mail with EmailFinisher
  • Loading branch information
bwaidelich authored Jun 16, 2020
2 parents 03b9b37 + bb50859 commit 65d6bf3
Showing 1 changed file with 77 additions and 19 deletions.
96 changes: 77 additions & 19 deletions Classes/Finishers/EmailFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*
* - templatePathAndFilename (mandatory if "templateSource" option is not set): Template path and filename for the mail body
* - templateSource (mandatory if "templatePathAndFilename" option is not set): The raw Fluid template
* - htmlTemplatePathAndFilename (mandatory if "htmlTemplateSource" option is not set): Template path and filename for the html mail body
* - htmlTemplateSource (mandatory if "htmlTemplatePathAndFilename" option is not set): The raw Fluid template for html mail body
* - plaintextTemplatePathAndFilename (mandatory if "plaintextTemplateSource" option is not set): Template path and filename for the plaintext mail body
* - plaintextTemplateSource (mandatory if "plaintextTemplatePathAndFilename" option is not set): The raw Fluid template for plaintext mail body
* - layoutRootPath: root path for the layouts
* - partialRootPath: root path for the partials
* - variables: associative array of variables which are available inside the Fluid template
Expand Down Expand Up @@ -54,13 +58,21 @@ class EmailFinisher extends AbstractFinisher
{
const FORMAT_PLAINTEXT = 'plaintext';
const FORMAT_HTML = 'html';
const FORMAT_MULTIPART = 'multipart';
const CONTENT_TYPE_PLAINTEXT = 'text/plain';
const CONTENT_TYPE_HTML = 'text/html';

/**
* @var Service
* @Flow\Inject
*/
protected $i18nService;

protected $formatContentTypes = [
self::FORMAT_HTML => self::CONTENT_TYPE_HTML,
self::FORMAT_PLAINTEXT => self::CONTENT_TYPE_PLAINTEXT,
];

/**
* @var array
*/
Expand All @@ -85,13 +97,6 @@ protected function executeInternal()
if (!class_exists(SwiftMailerMessage::class)) {
throw new FinisherException('The "neos/swiftmailer" doesn\'t seem to be installed, but is required for the EmailFinisher to work!', 1503392532);
}
$formRuntime = $this->finisherContext->getFormRuntime();
$standaloneView = $this->initializeStandaloneView();
$standaloneView->assign('form', $formRuntime);
$referrer = $formRuntime->getRequest()->getHttpRequest()->getUri();
$standaloneView->assign('referrer', $referrer);
$message = $standaloneView->render();

$subject = $this->parseOption('subject');
$recipientAddress = $this->parseOption('recipientAddress');
$recipientName = $this->parseOption('recipientName');
Expand All @@ -102,6 +107,7 @@ protected function executeInternal()
$blindCarbonCopyAddress = $this->parseOption('blindCarbonCopyAddress');
$format = $this->parseOption('format');
$testMode = $this->parseOption('testMode');
$messages = $this->getMessages($format);

if ($subject === null) {
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
Expand Down Expand Up @@ -140,11 +146,7 @@ protected function executeInternal()
$mail->setBcc($blindCarbonCopyAddress);
}

if ($format === self::FORMAT_PLAINTEXT) {
$mail->setBody($message, 'text/plain');
} else {
$mail->setBody($message, 'text/html');
}
$this->addMessages($mail, $messages);
$this->addAttachments($mail);

if ($testMode === true) {
Expand All @@ -155,7 +157,7 @@ protected function executeInternal()
'replyToAddress' => $replyToAddress,
'carbonCopyAddress' => $carbonCopyAddress,
'blindCarbonCopyAddress' => $blindCarbonCopyAddress,
'message' => $message,
'message' => $messages,
'format' => $format,
),
'E-Mail "' . $subject . '"'
Expand All @@ -165,20 +167,76 @@ protected function executeInternal()
}
}

protected function addMessages(SwiftMailerMessage $mail, array $messages): void
{
foreach ($messages as $messageFormat => $message) {
if (count($messages) === 1) {
$mail->setBody($message, $this->formatContentTypes[$messageFormat]);
} else {
$mail->addPart($message, $this->formatContentTypes[$messageFormat]);
}
}
}

protected function getMessages(string $format): array
{
$messages = [];
if ($format === self::FORMAT_MULTIPART) {
$messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML);
$messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT);
} elseif ($format === self::FORMAT_PLAINTEXT) {
$messages[self::FORMAT_PLAINTEXT] = $this->createMessage(self::FORMAT_PLAINTEXT);
} else {
$messages[self::FORMAT_HTML] = $this->createMessage(self::FORMAT_HTML);
}

return $messages;
}

protected function createMessage(string $format): string
{
$formRuntime = $this->finisherContext->getFormRuntime();
$standaloneView = $this->initializeStandaloneView($format);
$standaloneView->assign('form', $formRuntime);
$referrer = $formRuntime->getRequest()->getHttpRequest()->getUri();
$standaloneView->assign('referrer', $referrer);

return $standaloneView->render();
}

/**
* @param string $format
* @return StandaloneView
* @throws FinisherException
* @throws \Neos\FluidAdaptor\Exception
*/
protected function initializeStandaloneView()
protected function initializeStandaloneView(string $format = ''): StandaloneView
{
$templatePathAndFilenameOption = 'templatePathAndFilename';
$templateSourceOption = 'templateSource';
$isSingleTemplate = isset($this->options[$templatePathAndFilenameOption]) || isset($this->options[$templateSourceOption]);

if (!$isSingleTemplate && in_array($format, [self::FORMAT_PLAINTEXT, self::FORMAT_HTML])) {
$templatePathAndFilenameOption = $format . ucfirst($templatePathAndFilenameOption);
$templateSourceOption = $format . ucfirst($templateSourceOption);
}

$standaloneView = new StandaloneView();
if (isset($this->options['templatePathAndFilename'])) {
$templatePathAndFilename = $this->i18nService->getLocalizedFilename($this->options['templatePathAndFilename']);
if (isset($this->options[$templatePathAndFilenameOption])) {
$templatePathAndFilename = $this->i18nService->getLocalizedFilename($this->options[$templatePathAndFilenameOption]);
$standaloneView->setTemplatePathAndFilename($templatePathAndFilename[0]);
} elseif (isset($this->options['templateSource'])) {
$standaloneView->setTemplateSource($this->options['templateSource']);
} elseif (isset($this->options[$templateSourceOption])) {
$standaloneView->setTemplateSource($this->options[$templateSourceOption]);
} else {
throw new FinisherException('The option "templatePathAndFilename" or "templateSource" must be set for the EmailFinisher.', 1327058829);
$options = [
'templatePathAndFilename',
'templateSource',
self::FORMAT_PLAINTEXT . 'TemplatePathAndFilename',
self::FORMAT_PLAINTEXT . 'TemplateSource',
self::FORMAT_HTML . 'TemplatePathAndFilename',
self::FORMAT_HTML . 'TemplateSource'
];
throw new FinisherException(sprintf('One of the option "%s" must be set for the EmailFinisher.', implode('", "', $options)), 1551371435);
}


Expand Down

0 comments on commit 65d6bf3

Please sign in to comment.