Files
landing-api/app/Support/Inbox/Replier.php
Josep Piguillem 76d022053b Answer the inbox from the panel, with templates
A reply starts from a template filled in the language the person wrote in,
is sent by the application when it has a mailer that delivers or handed to
the replier's mail program otherwise, and is recorded on the submission.
The same is available through the API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 10:45:47 +02:00

62 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\Inbox;
use App\Enums\ApplicationStatus;
use App\Mail\ApplicationReplyMail;
use App\Models\Application;
use App\Models\ApplicationReply;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
/**
* Answering a submission: sending it when this application can send mail,
* handing it to the person's own mail program when it cannot, and keeping a
* record either way.
*
* "Can send" is a mailer that actually delivers. `log` and `array` accept a
* message and drop it, so with either of them a reply "sent from here" would
* be one nobody received.
*/
final class Replier
{
public static function canSendMail(): bool
{
return ! in_array(config('mail.default'), ['log', 'array', null], true);
}
public static function reply(Application $application, string $subject, string $body, User $by): ApplicationReply
{
$send = self::canSendMail();
$reply = $application->replies()->create([
'user_id' => $by->id,
'user_name' => $by->name,
'subject' => $subject,
'body' => $body,
'channel' => $send ? ApplicationReply::MAIL : ApplicationReply::MAILTO,
]);
if ($send) {
Mail::to($application->email, $application->name)->send(new ApplicationReplyMail($reply));
}
// Answered is at least contacted; a decision already taken stays.
if ($application->status === ApplicationStatus::New) {
$application->markAs(ApplicationStatus::Contacted, $by);
}
return $reply;
}
/**
* The same answer, for the person's own mail program to send.
*/
public static function mailto(Application $application, string $subject, string $body): string
{
return 'mailto:'.$application->email.'?subject='.rawurlencode($subject).'&body='.rawurlencode($body);
}
}