feat(mail-test): implement email testing functionality
- Created MailTestController to handle email sending requests. - Added SendTestMailRequest for validating email input. - Developed MailTestService to manage email sending logic. - Introduced TestMail Mailable for formatting test emails. - Defined API route for sending test emails. - Created EmailIntegrationSeeder to seed email integration settings. - Updated DatabaseSeeder to include EmailIntegrationSeeder. - Added MailTestControllerTest to ensure email sending functionality works as expected.
This commit is contained in:
26
app/Domains/MailTest/Controllers/MailTestController.php
Normal file
26
app/Domains/MailTest/Controllers/MailTestController.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Controllers;
|
||||
|
||||
use App\Domains\MailTest\Requests\SendTestMailRequest;
|
||||
use App\Domains\MailTest\Services\MailTestService;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class MailTestController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected MailTestService $mailTestService,
|
||||
) {}
|
||||
|
||||
public function __invoke(SendTestMailRequest $request): JsonResponse
|
||||
{
|
||||
return response()->json(
|
||||
$this->mailTestService->send(
|
||||
$request->validated('to'),
|
||||
$request->validated('subject'),
|
||||
$request->validated('message'),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
37
app/Domains/MailTest/Mailables/TestMail.php
Normal file
37
app/Domains/MailTest/Mailables/TestMail.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Mailables;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TestMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly string $mailSubject,
|
||||
public readonly string $mailMessage,
|
||||
) {}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(subject: $this->mailSubject);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
$message = nl2br(e($this->mailMessage));
|
||||
|
||||
return new Content(
|
||||
htmlString: <<<HTML
|
||||
<h1>Prueba de correo de Shopit</h1>
|
||||
<p>{$message}</p>
|
||||
<p><small>Si recibiste este mensaje, la configuracion de correo funciona correctamente.</small></p>
|
||||
HTML,
|
||||
);
|
||||
}
|
||||
}
|
||||
25
app/Domains/MailTest/Requests/SendTestMailRequest.php
Normal file
25
app/Domains/MailTest/Requests/SendTestMailRequest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SendTestMailRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'to' => ['required', 'string', 'email', 'max:255'],
|
||||
'subject' => ['nullable', 'string', 'max:255'],
|
||||
'message' => ['nullable', 'string', 'max:5000'],
|
||||
];
|
||||
}
|
||||
}
|
||||
27
app/Domains/MailTest/Services/MailTestService.php
Normal file
27
app/Domains/MailTest/Services/MailTestService.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\MailTest\Services;
|
||||
|
||||
use App\Domains\MailTest\Mailables\TestMail;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class MailTestService
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function send(string $recipient, ?string $subject = null, ?string $message = null): array
|
||||
{
|
||||
$subject ??= 'Prueba de correo de Shopit';
|
||||
$message ??= 'Este es un correo de prueba enviado desde Shopit.';
|
||||
|
||||
Mail::to($recipient)->send(new TestMail($subject, $message));
|
||||
|
||||
return [
|
||||
'message' => 'Correo de prueba enviado correctamente.',
|
||||
'recipient' => $recipient,
|
||||
'mailer' => (string) config('mail.default'),
|
||||
'sent_at' => now()->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
6
app/Domains/MailTest/routes/api.php
Normal file
6
app/Domains/MailTest/routes/api.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Domains\MailTest\Controllers\MailTestController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::post('mail-test/send', MailTestController::class);
|
||||
Reference in New Issue
Block a user