feat(contact): email submissions to support and owner inbox
Deploy Backend / deploy (push) Successful in 3m53s
Deploy Backend / deploy (push) Successful in 3m53s
Store contact messages as before, then notify support@parsaaghayi.ir and parsa.aghaee@gmail.com via SMTP-ready mail config. Failures are logged without blocking the 201 response; PHPUnit covers both recipients.
This commit is contained in:
1 parent
eda889e166
commit
1c8086bad1
7 files changed
+147
-12
No files matched your search
+8
-7
@@ -46,14 +46,15 @@ REDIS_HOST=127.0.0.1
|
||||
REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
|
||||
MAIL_MAILER=log
|
||||
MAIL_HOST=127.0.0.1
|
||||
MAIL_PORT=2525
|
||||
MAIL_USERNAME=null
|
||||
MAIL_PASSWORD=null
|
||||
MAIL_ENCRYPTION=null
|
||||
MAIL_FROM_ADDRESS="hello@example.com"
|
||||
MAIL_MAILER=smtp
|
||||
MAIL_HOST=mail.parsaaghayi.ir
|
||||
MAIL_PORT=465
|
||||
MAIL_USERNAME=support-parsaaghayi
|
||||
MAIL_PASSWORD=
|
||||
MAIL_ENCRYPTION=ssl
|
||||
MAIL_FROM_ADDRESS="support@parsaaghayi.ir"
|
||||
MAIL_FROM_NAME="${APP_NAME}"
|
||||
CONTACT_NOTIFY_TO=support@parsaaghayi.ir,parsa.aghaee@gmail.com
|
||||
|
||||
AWS_ACCESS_KEY_ID=
|
||||
AWS_SECRET_ACCESS_KEY=
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Mail\ContactMessageReceived;
|
||||
use App\Models\ContactMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
/**
|
||||
* @OA\Tag(name="Contact", description="Contact form submissions")
|
||||
@@ -15,8 +18,8 @@ class ContactMessageController extends Controller
|
||||
/**
|
||||
* Public contact form submission.
|
||||
*
|
||||
* The site has no mail service of its own; messages are stored in the
|
||||
* database and read from the admin panel instead of being emailed.
|
||||
* Messages are stored for the admin panel and emailed to the support
|
||||
* mailbox plus the owner notification address.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
@@ -32,6 +35,18 @@ class ContactMessageController extends Controller
|
||||
'message' => $validated['message'],
|
||||
]);
|
||||
|
||||
$recipients = config('contact.notify', []);
|
||||
if ($recipients !== []) {
|
||||
try {
|
||||
Mail::to($recipients)->send(new ContactMessageReceived($message));
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Contact form notification email failed', [
|
||||
'contact_message_id' => $message->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'پیام شما با موفقیت ثبت شد.'], 201);
|
||||
}
|
||||
|
||||
@@ -67,4 +82,4 @@ class ContactMessageController extends Controller
|
||||
|
||||
return response()->json(null, 200);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\ContactMessage;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Address;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ContactMessageReceived extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public function __construct(public ContactMessage $contactMessage)
|
||||
{
|
||||
}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'پیام جدید از فرم تماس پارساآقایی',
|
||||
replyTo: [
|
||||
new Address(
|
||||
$this->contactMessage->email,
|
||||
$this->contactMessage->name
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
html: 'mail.contact-message',
|
||||
text: 'mail.contact-message-text',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Contact form notification recipients
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Comma-separated list of addresses that receive every public contact
|
||||
| submission. Defaults cover the support mailbox and the owner inbox.
|
||||
|
|
||||
*/
|
||||
|
||||
'notify' => array_values(array_filter(array_map(
|
||||
'trim',
|
||||
explode(',', (string) env(
|
||||
'CONTACT_NOTIFY_TO',
|
||||
'support@parsaaghayi.ir,parsa.aghaee@gmail.com'
|
||||
))
|
||||
))),
|
||||
|
||||
];
|
||||
@@ -0,0 +1,8 @@
|
||||
پیام جدید از فرم تماس پارساآقایی
|
||||
|
||||
نام: {{ $contactMessage->name }}
|
||||
ایمیل: {{ $contactMessage->email }}
|
||||
شناسه: #{{ $contactMessage->id }}
|
||||
|
||||
پیام:
|
||||
{{ $contactMessage->message }}
|
||||
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fa" dir="rtl">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>پیام جدید از فرم تماس</title>
|
||||
</head>
|
||||
<body style="font-family: Tahoma, Arial, sans-serif; line-height: 1.7; color: #111;">
|
||||
<h2 style="margin-bottom: 8px;">پیام جدید از فرم تماس پارساآقایی</h2>
|
||||
<p style="margin: 0 0 16px;">یک نفر از طریق سایت پیام گذاشته است.</p>
|
||||
|
||||
<table cellpadding="8" cellspacing="0" style="border-collapse: collapse; width: 100%; max-width: 640px;">
|
||||
<tr>
|
||||
<td style="border: 1px solid #ddd; width: 120px; background: #f7f7f7;"><strong>نام</strong></td>
|
||||
<td style="border: 1px solid #ddd;">{{ $contactMessage->name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #ddd; background: #f7f7f7;"><strong>ایمیل</strong></td>
|
||||
<td style="border: 1px solid #ddd;" dir="ltr">{{ $contactMessage->email }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #ddd; background: #f7f7f7; vertical-align: top;"><strong>پیام</strong></td>
|
||||
<td style="border: 1px solid #ddd; white-space: pre-wrap;">{{ $contactMessage->message }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #ddd; background: #f7f7f7;"><strong>شناسه</strong></td>
|
||||
<td style="border: 1px solid #ddd;" dir="ltr">#{{ $contactMessage->id }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p style="margin-top: 20px; font-size: 13px; color: #555;">
|
||||
این ایمیل بهصورت خودکار پس از ثبت فرم تماس ارسال شده است.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Mail\ContactMessageReceived;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ContactMessageTest extends TestCase
|
||||
@@ -13,6 +15,8 @@ class ContactMessageTest extends TestCase
|
||||
|
||||
public function test_the_public_form_stores_a_message(): void
|
||||
{
|
||||
Mail::fake();
|
||||
|
||||
$response = $this->postJson('/api/contact', [
|
||||
'name' => 'Ali',
|
||||
'email' => 'ali@example.com',
|
||||
@@ -25,15 +29,24 @@ class ContactMessageTest extends TestCase
|
||||
'email' => 'ali@example.com',
|
||||
'read_at' => null,
|
||||
]);
|
||||
|
||||
Mail::assertSent(ContactMessageReceived::class, function (ContactMessageReceived $mail) {
|
||||
return $mail->contactMessage->email === 'ali@example.com'
|
||||
&& $mail->hasTo('support@parsaaghayi.ir')
|
||||
&& $mail->hasTo('parsa.aghaee@gmail.com');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_the_public_form_rejects_invalid_payloads(): void
|
||||
{
|
||||
Mail::fake();
|
||||
|
||||
$this->postJson('/api/contact', ['name' => '', 'email' => 'not-an-email', 'message' => ''])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['name', 'email', 'message']);
|
||||
|
||||
$this->assertDatabaseCount('contact_messages', 0);
|
||||
Mail::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_anonymous_requests_cannot_manage_messages(): void
|
||||
@@ -113,4 +126,4 @@ class ContactMessageTest extends TestCase
|
||||
$this->patchJson('/api/admin/messages/'.$message->id, ['read' => true])->assertForbidden();
|
||||
$this->deleteJson('/api/admin/messages/'.$message->id)->assertForbidden();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user