1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\EmailBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use ProjetNormandie\EmailBundle\Mapper\MailerMapper; |
7
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
8
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
9
|
|
|
use Symfony\Component\Mime\Email; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Proxy to use the Swift_Mailer with the user class SwiftMailerMapper. |
13
|
|
|
*/ |
14
|
|
|
class Mailer |
15
|
|
|
{ |
16
|
|
|
protected MailerInterface $mailer; |
17
|
|
|
protected MailerMapper $mapper; |
18
|
|
|
private EntityManagerInterface $em; |
19
|
|
|
private string $from; |
20
|
|
|
private string $to; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param MailerInterface $mailer |
24
|
|
|
* @param MailerMapper $mapper |
25
|
|
|
* @param EntityManagerInterface $em |
26
|
|
|
* @param string $pnEmailFrom |
27
|
|
|
* @param string $pnEmailTo |
28
|
|
|
*/ |
29
|
|
|
public function __construct(MailerInterface $mailer, MailerMapper $mapper, EntityManagerInterface $em, string $pnEmailFrom, string $pnEmailTo) |
30
|
|
|
{ |
31
|
|
|
$this->mailer = $mailer; |
32
|
|
|
$this->mapper = $mapper; |
33
|
|
|
$this->em = $em; |
34
|
|
|
$this->from = $pnEmailFrom; |
35
|
|
|
$this->to = $pnEmailTo; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param String $subject |
40
|
|
|
* @param String $body |
41
|
|
|
* @param String|null $from |
42
|
|
|
* @param String|null $to |
43
|
|
|
* @return void |
44
|
|
|
* @throws TransportExceptionInterface |
45
|
|
|
*/ |
46
|
|
|
public function send(String $subject, String $body, String $from = null, String $to = null) |
47
|
|
|
{ |
48
|
|
|
$email = new Email(); |
49
|
|
|
$email->subject($subject); |
50
|
|
|
$email->html($body); |
51
|
|
|
$email->text($body); |
52
|
|
|
|
53
|
|
|
if ($from !== null) { |
54
|
|
|
$email->replyTo($from); |
55
|
|
|
} |
56
|
|
|
$email->from($this->from); |
57
|
|
|
|
58
|
|
|
if (null !== $to) { |
59
|
|
|
$email->to($to); |
60
|
|
|
} else { |
61
|
|
|
$email->to($this->to); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->mailer->send($email); |
65
|
|
|
|
66
|
|
|
$entity = $this->mapper->fromEmail($email); |
67
|
|
|
$this->em->persist($entity); |
68
|
|
|
$this->em->flush(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|