2024-10-22 10:34:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Notifications\Forms;
|
|
|
|
|
|
|
|
|
|
use App\Events\Forms\FormSubmitted;
|
|
|
|
|
use App\Open\MentionParser;
|
|
|
|
|
use App\Service\Forms\FormSubmissionFormatter;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
|
use Illuminate\Notifications\Notification;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Vinkla\Hashids\Facades\Hashids;
|
|
|
|
|
use Symfony\Component\Mime\Email;
|
|
|
|
|
|
|
|
|
|
class FormEmailNotification extends Notification implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
|
|
public FormSubmitted $event;
|
|
|
|
|
public string $mailer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new notification instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(FormSubmitted $event, private $integrationData, string $mailer)
|
|
|
|
|
{
|
|
|
|
|
$this->event = $event;
|
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the notification's delivery channels.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $notifiable
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function via($notifiable)
|
|
|
|
|
{
|
|
|
|
|
return ['mail'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the mail representation of the notification.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $notifiable
|
|
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
|
|
|
*/
|
|
|
|
|
public function toMail($notifiable)
|
|
|
|
|
{
|
|
|
|
|
return (new MailMessage())
|
|
|
|
|
->mailer($this->mailer)
|
2024-10-28 10:17:01 +01:00
|
|
|
->replyTo($this->getReplyToEmail($this->event->form->creator->email))
|
2024-10-22 10:34:29 +02:00
|
|
|
->from($this->getFromEmail(), $this->getSenderName())
|
|
|
|
|
->subject($this->getSubject())
|
|
|
|
|
->withSymfonyMessage(function (Email $message) {
|
|
|
|
|
$this->addCustomHeaders($message);
|
|
|
|
|
})
|
|
|
|
|
->markdown('mail.form.email-notification', $this->getMailData());
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 10:17:01 +01:00
|
|
|
private function formatSubmissionData($createLinks = true): array
|
2024-10-22 10:34:29 +02:00
|
|
|
{
|
|
|
|
|
$formatter = (new FormSubmissionFormatter($this->event->form, $this->event->data))
|
|
|
|
|
->outputStringsOnly()
|
|
|
|
|
->useSignedUrlForFiles();
|
|
|
|
|
|
2024-10-28 10:17:01 +01:00
|
|
|
if ($createLinks) {
|
|
|
|
|
$formatter->createLinks();
|
|
|
|
|
}
|
2024-10-22 10:34:29 +02:00
|
|
|
if ($this->integrationData->include_hidden_fields_submission_data ?? false) {
|
|
|
|
|
$formatter->showHiddenFields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $formatter->getFieldsWithValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getFromEmail(): string
|
|
|
|
|
{
|
|
|
|
|
if (
|
|
|
|
|
config('app.self_hosted')
|
|
|
|
|
&& isset($this->integrationData->sender_email)
|
|
|
|
|
&& $this->validateEmail($this->integrationData->sender_email)
|
|
|
|
|
) {
|
|
|
|
|
return $this->integrationData->sender_email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config('mail.from.address');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getSenderName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->integrationData->sender_name ?? config('app.name');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getReplyToEmail($default): string
|
|
|
|
|
{
|
|
|
|
|
$replyTo = $this->integrationData->reply_to ?? null;
|
|
|
|
|
if ($replyTo) {
|
|
|
|
|
$parsedReplyTo = $this->parseReplyTo($replyTo);
|
|
|
|
|
if ($parsedReplyTo && $this->validateEmail($parsedReplyTo)) {
|
|
|
|
|
return $parsedReplyTo;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-28 10:17:01 +01:00
|
|
|
return $default;
|
2024-10-22 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function parseReplyTo(string $replyTo): ?string
|
|
|
|
|
{
|
2024-10-28 10:17:01 +01:00
|
|
|
$parser = new MentionParser($replyTo, $this->formatSubmissionData(false));
|
2024-10-22 10:34:29 +02:00
|
|
|
return $parser->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getSubject(): string
|
|
|
|
|
{
|
|
|
|
|
$defaultSubject = 'New form submission';
|
2024-10-28 10:17:01 +01:00
|
|
|
$parser = new MentionParser($this->integrationData->subject ?? $defaultSubject, $this->formatSubmissionData(false));
|
2024-10-22 10:34:29 +02:00
|
|
|
return $parser->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function addCustomHeaders(Email $message): void
|
|
|
|
|
{
|
|
|
|
|
$formId = $this->event->form->id;
|
|
|
|
|
$submissionId = $this->event->data['submission_id'] ?? 'unknown';
|
|
|
|
|
$domain = Str::after(config('app.url'), '://');
|
2024-10-22 11:04:49 +02:00
|
|
|
$timestamp = time();
|
2024-10-22 10:34:29 +02:00
|
|
|
|
2024-10-22 11:04:49 +02:00
|
|
|
// Create a unique Message-ID for each submission
|
|
|
|
|
$messageId = "<form-{$formId}-submission-{$submissionId}-{$timestamp}@{$domain}>";
|
2024-10-22 10:34:29 +02:00
|
|
|
|
2024-10-22 11:04:49 +02:00
|
|
|
// Create a References header that links to the form, but not to specific submissions
|
|
|
|
|
$references = "<form-{$formId}@{$domain}>";
|
|
|
|
|
|
|
|
|
|
// Add our custom Message-ID as X-Custom-Message-ID
|
|
|
|
|
$message->getHeaders()->addTextHeader('X-Custom-Message-ID', $messageId);
|
|
|
|
|
|
|
|
|
|
// Add References header
|
2024-10-22 10:34:29 +02:00
|
|
|
$message->getHeaders()->addTextHeader('References', $references);
|
2024-10-22 11:04:49 +02:00
|
|
|
|
|
|
|
|
// Add a unique Thread-Index to further prevent grouping
|
|
|
|
|
$threadIndex = base64_encode(pack('H*', md5($formId . $submissionId . $timestamp)));
|
|
|
|
|
$message->getHeaders()->addTextHeader('Thread-Index', $threadIndex);
|
2024-10-22 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMailData(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'emailContent' => $this->getEmailContent(),
|
2024-10-28 10:17:01 +01:00
|
|
|
'fields' => $this->formatSubmissionData(),
|
2024-10-22 10:34:29 +02:00
|
|
|
'form' => $this->event->form,
|
|
|
|
|
'integrationData' => $this->integrationData,
|
|
|
|
|
'noBranding' => $this->event->form->no_branding,
|
|
|
|
|
'submission_id' => $this->getEncodedSubmissionId(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getEmailContent(): string
|
|
|
|
|
{
|
2024-10-28 10:17:01 +01:00
|
|
|
$parser = new MentionParser($this->integrationData->email_content ?? '', $this->formatSubmissionData());
|
2024-10-22 10:34:29 +02:00
|
|
|
return $parser->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getEncodedSubmissionId(): ?string
|
|
|
|
|
{
|
|
|
|
|
$submissionId = $this->event->data['submission_id'] ?? null;
|
|
|
|
|
return $submissionId ? Hashids::encode($submissionId) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function validateEmail($email): bool
|
|
|
|
|
{
|
|
|
|
|
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
|
}
|
|
|
|
|
}
|