49 lines
1.6 KiB
PHP
Executable File
49 lines
1.6 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Evomedien\Vitec\Forms\Delivery;
|
|
|
|
use TYPO3\CMS\Core\Mail\MailMessage;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
/**
|
|
* Sends the submission as a plain-text mail to the FlexForm-configured
|
|
* recipient(s). Sender = system default (MAIL settings); reply-to = the
|
|
* submitter's email when present.
|
|
*/
|
|
final class EmailDelivery implements DeliveryInterface
|
|
{
|
|
public function deliver(string $formKey, array $payload, array $settings): void
|
|
{
|
|
$recipients = GeneralUtility::trimExplode(',', (string)($settings['recipient'] ?? ''), true);
|
|
if ($recipients === []) {
|
|
throw new \RuntimeException('No recipient configured in the form plugin (FlexForm "Recipient").');
|
|
}
|
|
|
|
$subject = trim((string)($settings['subject'] ?? ''));
|
|
if ($subject === '') {
|
|
$subject = 'VITEC website form: ' . $formKey;
|
|
}
|
|
|
|
$lines = [];
|
|
foreach ($payload as $field => $value) {
|
|
$lines[] = str_pad($field . ':', 16) . $value;
|
|
}
|
|
|
|
$mail = GeneralUtility::makeInstance(MailMessage::class);
|
|
$mail->to(...$recipients)
|
|
->subject($subject)
|
|
->text("Form: $formKey\n\n" . implode("\n", $lines));
|
|
|
|
$submitterEmail = trim((string)($payload['email'] ?? ''));
|
|
if ($submitterEmail !== '' && filter_var($submitterEmail, FILTER_VALIDATE_EMAIL)) {
|
|
$mail->replyTo($submitterEmail);
|
|
}
|
|
|
|
if (!$mail->send()) {
|
|
throw new \RuntimeException('MailMessage::send() reported failure.');
|
|
}
|
|
}
|
|
}
|