new: usr: add Contact page with email sending behaviour #4
All checks were successful
Deploy to Production / deploy (push) Successful in 39s
All checks were successful
Deploy to Production / deploy (push) Successful in 39s
This commit is contained in:
@@ -10,10 +10,19 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\ContactMessage;
|
||||
use App\Form\ContactFormType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
@@ -31,11 +40,14 @@ class GameController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'APP_ENV')]
|
||||
private readonly string $env,
|
||||
private readonly string $env,
|
||||
#[Autowire(env: 'MERCURE_PUBLIC_URL')]
|
||||
private readonly string $mercurePublicUrl,
|
||||
private readonly string $mercurePublicUrl,
|
||||
#[Autowire(env: 'MERCURE_SUBSCRIBER_JWT')]
|
||||
private readonly string $mercureSubscriberJwt,
|
||||
private readonly string $mercureSubscriberJwt,
|
||||
#[Autowire(env: 'APP_CONTACT_MAIL_ADDRESS')]
|
||||
private readonly string $appContactMailAddress,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -69,9 +81,28 @@ class GameController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/contact', name: 'MineSeekerBundle_contact')]
|
||||
public function contact(): Response
|
||||
{
|
||||
return $this->render('Official/contact.html.twig');
|
||||
public function contact(
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
MailerInterface $mailer,
|
||||
): Response {
|
||||
$contactMessage = new ContactMessage();
|
||||
$form = $this->createForm(ContactFormType::class, $contactMessage);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$contactMessage->setIpAddress($request->getClientIp());
|
||||
$em->persist($contactMessage);
|
||||
$em->flush();
|
||||
$this->sendMail($mailer, $contactMessage);
|
||||
$this->addFlash('contact_success', 'Thank you for your message! We will get back to you soon.');
|
||||
|
||||
return $this->redirectToRoute('MineSeekerBundle_contact');
|
||||
}
|
||||
|
||||
return $this->render('Official/contact.html.twig', [
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/landing-page', name: 'MineSeekerBundle_landing')]
|
||||
@@ -79,4 +110,31 @@ class GameController extends AbstractController
|
||||
{
|
||||
return $this->render('Official/landing.html.twig');
|
||||
}
|
||||
|
||||
public function sendMail(MailerInterface $mailer, ContactMessage $contactMessage): void
|
||||
{
|
||||
try {
|
||||
$mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($this->appContactMailAddress)
|
||||
->replyTo($contactMessage->getEmail())
|
||||
->subject('New Contact Message from ' . $contactMessage->getName())
|
||||
->htmlTemplate('emails/contact_notification.html.twig')
|
||||
->context(['message' => $contactMessage])
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Failed to send contact notification email: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
'message' => $contactMessage,
|
||||
]);
|
||||
throw new RuntimeException('Failed to send contact notification email: ' . $e->getMessage());
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error('Failed to send contact notification email: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
'message' => $contactMessage,
|
||||
]);
|
||||
throw new RuntimeException('Failed to send contact notification email: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user