60 lines
2.1 KiB
PHP
Executable File
60 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Services\EmailService;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ContactController extends BaseApiController
|
|
{
|
|
public function submit()
|
|
{
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$rules = [
|
|
'name' => 'required|min_length[3]',
|
|
'email' => 'required|valid_email',
|
|
'subject' => 'required|min_length[3]',
|
|
'message' => 'required|min_length[10]',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
$name = trim((string) ($data['name'] ?? ''));
|
|
$email = strtolower(trim((string) ($data['email'] ?? '')));
|
|
$subject = trim((string) ($data['subject'] ?? ''));
|
|
$message = trim((string) ($data['message'] ?? ''));
|
|
|
|
$body = "<p><strong>From:</strong> {$name} ({$email})</p>"
|
|
. "<p><strong>Subject:</strong> {$subject}</p>"
|
|
. "<p>{$message}</p>";
|
|
|
|
$supportEmail = env('SUPPORT_EMAIL', 'support@alrahmaisgl.org');
|
|
|
|
try {
|
|
/** @var EmailService $mailer */
|
|
$mailer = app(EmailService::class);
|
|
$sent = $mailer->setFrom($email, $name)
|
|
->setTo($supportEmail)
|
|
->setSubject($subject)
|
|
->setMessage($body)
|
|
->send();
|
|
|
|
if (!$sent) {
|
|
return $this->respondError('There was an error sending your message. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
return $this->success(null, 'Thank you for contacting us! We will get back to you soon.');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Contact form submission error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to submit contact form', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|