add contact form and mailing
Co-authored-by: Constantin Simonis <constantin@simonis.lol> Reviewed-on: http://git.simonis.lol/sites/abiball/pulls/6 Reviewed-by: Constantin Simonis <constantin@simonis.lol> Co-authored-by: Jan-Marlon Leibl <jleibl@proton.me> Co-committed-by: Jan-Marlon Leibl <jleibl@proton.me>
This commit is contained in:
32
src/Controller/ContactController.php
Normal file
32
src/Controller/ContactController.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\DataObjects\ContactData;
|
||||
use App\Form\ContactForm;
|
||||
use App\Service\ContactService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class ContactController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/contact', name: 'app_contact', methods: ['GET', 'POST'])]
|
||||
public function index(Request $request, ContactService $service): Response
|
||||
{
|
||||
$data = new ContactData();
|
||||
$form = $this->createForm(ContactForm::class, $data)->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$service->send($data);
|
||||
noty()->success('Deine Nachricht wurde erfolgreich versendet!');
|
||||
|
||||
return $this->redirectToRoute('app_contact');
|
||||
}
|
||||
|
||||
return $this->render('contact/index.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
13
src/DataObjects/ContactData.php
Normal file
13
src/DataObjects/ContactData.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\DataObjects;
|
||||
|
||||
class ContactData
|
||||
{
|
||||
public function __construct(
|
||||
public string $email = '',
|
||||
public string $message = '',
|
||||
public ?string $phone = null,
|
||||
) {
|
||||
}
|
||||
}
|
19
src/Form/ContactForm.php
Normal file
19
src/Form/ContactForm.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class ContactForm extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->add('email', EmailType::class)
|
||||
->add('message', TextareaType::class)
|
||||
->add('phone', TextType::class);
|
||||
}
|
||||
}
|
30
src/Service/ContactService.php
Normal file
30
src/Service/ContactService.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DataObjects\ContactData;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
|
||||
final readonly class ContactService
|
||||
{
|
||||
public function __construct(
|
||||
private MailerInterface $mailer,
|
||||
#[Autowire(env: 'CONTACT_MAIL')]
|
||||
private string $contactEmail,
|
||||
){
|
||||
}
|
||||
|
||||
public function send(ContactData $data): void
|
||||
{
|
||||
$mail = (new TemplatedEmail())
|
||||
->htmlTemplate('email/contact.html.twig')
|
||||
->context(['data' => $data])
|
||||
->subject('Kontakt aufnahme')
|
||||
->from($data->email)
|
||||
->to($this->contactEmail);
|
||||
|
||||
$this->mailer->send($mail);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user