Files
plugi/extensions/plugi-ext-form/admin/Classes/Finishers/SendEmailToAdmin.php
T
deepDiverPaul d5597b045f add configurable form finishers and webhook delivery
Expose finisher settings through the editor registry and build the action editor dynamically. Add configurable admin email recipients and subjects, and introduce a webhook finisher with validated request options and JSON submission payloads.

Expand submission handler tests to cover editor configuration, email overrides, webhook validation, HTTP responses, and submission outcomes.
2026-11-24 18:51:20 +01:00

53 lines
1.6 KiB
PHP

<?php
namespace Plugi\Extensions\Form\Finishers;
use Plugi\Extensions;
class SendEmailToAdmin extends DefaultFinisher
{
public function execute(): bool
{
$settings = Extensions::getSettings('form');
$to = $this->options['recipient'] ?? '';
if (!is_string($to)) {
return false;
}
if (trim($to) === '') {
$to = $settings['email']['value'] ?? null;
}
if (!is_string($to) || filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
return false;
}
$subject = $this->options['subject'] ?? '';
if (!is_string($subject)) {
return false;
}
if (trim($subject) === '') {
$subject = 'Form submission: '.$this->getFormName();
}
if (strpbrk($subject, "\r\n") !== false) {
return false;
}
$message = $this->getNiceFields();
$mailer = phpb_instance('mail');
return $mailer !== null && $mailer->send($to, $subject, $message) === true;
}
private function getNiceFields(): string {
$text = '';
foreach ($this->getFormFields() as $field) {
$name = $field['name'] ?? '';
$label = $field['label'] ?? $name;
$value = $this->getEntryFields()[$name] ?? '';
if (is_array($value)) {
$value = implode(', ', array_map('strval', $value));
} elseif (!is_scalar($value)) {
$value = '';
}
$text .= $label . ': ' . $value . "\n";
}
return $text;
}
}