62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Email;
|
|
|
|
use App\Http\Controllers\Api\Core\BaseApiController;
|
|
use App\Http\Requests\Email\EmailSendRequest;
|
|
use App\Http\Resources\Email\EmailSenderResource;
|
|
use App\Services\Email\EmailAttachmentService;
|
|
use App\Services\Email\EmailDispatchService;
|
|
use App\Services\Email\EmailSenderOptionsService;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class EmailController extends BaseApiController
|
|
{
|
|
public function __construct(
|
|
private EmailDispatchService $dispatch,
|
|
private EmailSenderOptionsService $senderOptions,
|
|
private EmailAttachmentService $attachments
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function senders(): JsonResponse
|
|
{
|
|
$senders = $this->senderOptions->listSenders();
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'senders' => EmailSenderResource::collection($senders),
|
|
]);
|
|
}
|
|
|
|
public function send(EmailSendRequest $request): JsonResponse
|
|
{
|
|
$payload = $request->validated();
|
|
|
|
$attachments = $this->attachments->normalize($request->file('attachments', []));
|
|
|
|
$ok = $this->dispatch->send(
|
|
$payload['recipient'],
|
|
$payload['subject'],
|
|
$payload['html_message'],
|
|
$payload['profile'] ?? null,
|
|
$payload['reply_to_email'] ?? null,
|
|
$payload['reply_to_name'] ?? null,
|
|
$attachments
|
|
);
|
|
|
|
if (!$ok) {
|
|
return response()->json([
|
|
'ok' => false,
|
|
'message' => 'Email failed to send.',
|
|
], 500);
|
|
}
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'message' => 'Email sent successfully.',
|
|
]);
|
|
}
|
|
}
|