Changes to documents able to edit them now, also support for auto mail attechemnts

This commit is contained in:
Simon Pocrnjič
2025-10-18 19:04:10 +02:00
parent 761799bdbe
commit 3b1a24287a
19 changed files with 820 additions and 108 deletions
+19 -1
View File
@@ -22,7 +22,7 @@ public function __construct(public EmailTemplateRenderer $renderer) {}
* Attempt to queue an auto mail for the given activity based on its decision/template.
* Returns array with either ['queued' => true, 'log_id' => int] or ['skipped' => 'reason'].
*/
public function maybeQueue(Activity $activity, bool $sendFlag = true): array
public function maybeQueue(Activity $activity, bool $sendFlag = true, array $options = []): array
{
$decision = $activity->decision;
if (! $sendFlag || ! $decision || ! $decision->auto_mail || ! $decision->email_template_id) {
@@ -114,6 +114,24 @@ public function maybeQueue(Activity $activity, bool $sendFlag = true): array
if (count($recipients) > 1) {
$log->to_email = null;
}
// Resolve and attach selected documents as attachments metadata (id, name, path, mime, size)
$attachmentIds = collect($options['attachment_ids'] ?? [])->filter()->map(fn ($v) => (int) $v)->values();
if ($attachmentIds->isNotEmpty()) {
$docs = \App\Models\Document::query()
->whereIn('id', $attachmentIds)
->get(['id', 'disk', 'path', 'original_name', 'name', 'mime_type', 'size']);
$log->attachments = $docs->map(function ($d) {
return [
'id' => $d->id,
'disk' => $d->disk ?: 'public',
'path' => $d->path,
'name' => $d->original_name ?: ($d->name ?: basename($d->path)),
'mime' => $d->mime_type ?: 'application/octet-stream',
'size' => $d->size,
];
})->values()->all();
}
$log->save();
$log->body()->create([
+24 -4
View File
@@ -149,14 +149,14 @@ public function sendFromLog(EmailLog $log): array
if ($singleTo === '' || ! filter_var($singleTo, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('No valid recipient email found for EmailLog #'.$log->id);
}
$email->to(new Address($singleTo, (string) ($log->to_name ?? '')));
$email->to(new Address($singleTo, (string) ($log->to_name ?? '')));
}
// Always BCC the sender mailbox if present and not already in To
$senderBcc = null;
if ($fromAddr !== '' && filter_var($fromAddr, FILTER_VALIDATE_EMAIL)) {
// Check duplicates against toList
$lowerTo = array_map(fn($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
$lowerTo = array_map(fn ($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
if (! in_array(strtolower($fromAddr), $lowerTo, true)) {
$senderBcc = $fromAddr;
$email->bcc(new Address($senderBcc));
@@ -175,6 +175,26 @@ public function sendFromLog(EmailLog $log): array
$email->replyTo($log->reply_to);
}
// Attach files if present on the log
$attachments = (array) ($log->attachments ?? []);
foreach ($attachments as $att) {
try {
$disk = $att['disk'] ?? 'public';
$path = $att['path'] ?? null;
if (! $path) {
continue;
}
$name = $att['name'] ?? basename($path);
$mime = $att['mime'] ?? 'application/octet-stream';
$full = \Storage::disk($disk)->path($path);
if (is_file($full)) {
$email->attachFromPath($full, $name, $mime);
}
} catch (\Throwable $e) {
// ignore individual attachment failures; continue sending
}
}
$mailer->send($email);
// Save log if we modified BCC
if (! empty($log->getAttribute('bcc'))) {
@@ -205,7 +225,7 @@ public function sendFromLog(EmailLog $log): array
// BCC the sender mailbox if resolvable and not already in To
$fromAddr = (string) ($log->from_email ?: (config('mail.from.address') ?? ''));
if ($fromAddr !== '' && filter_var($fromAddr, FILTER_VALIDATE_EMAIL)) {
$lowerTo = array_map(fn($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
$lowerTo = array_map(fn ($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
if (! in_array(strtolower($fromAddr), $lowerTo, true)) {
$message->bcc($fromAddr);
$log->bcc = [$fromAddr];
@@ -240,7 +260,7 @@ public function sendFromLog(EmailLog $log): array
// BCC the sender mailbox if resolvable and not already in To
$fromAddr = (string) ($log->from_email ?: (config('mail.from.address') ?? ''));
if ($fromAddr !== '' && filter_var($fromAddr, FILTER_VALIDATE_EMAIL)) {
$lowerTo = array_map(fn($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
$lowerTo = array_map(fn ($v) => strtolower(trim((string) $v)), (array) ($log->to_recipients ?? [$log->to_email]));
if (! in_array(strtolower($fromAddr), $lowerTo, true)) {
$message->bcc($fromAddr);
$log->bcc = [$fromAddr];