-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailController.php
52 lines (42 loc) · 1.7 KB
/
EmailController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Form\NewsletterSubscriptionType;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Psr\Log\LoggerInterface;
#[Route('/email')]
class EmailController extends AbstractController
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
#[Route('/subscribe-newsletter', name: 'app_subscribe_newsletter')]
public function subscribeNewsletter(Request $request, LoggerInterface $logger): Response
{
$form = $this->createForm(NewsletterSubscriptionType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$emailAddress = $form->get('email')->getData();
try {
$email = (new Email())
->from('hello@example.com')
->to($emailAddress)
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$this->mailer->send($email);
} catch (TransportException $th) {
$logger->error($th->getMessage());
}
return $this->redirectToRoute('app_home_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('email/subscribeNewsletter.html.twig', ['form' => $form]);
}
}