|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
6
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Mailer |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \Swift_Mailer |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $mailer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var UrlGeneratorInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $router; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EngineInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $templating; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $fromEmail; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fromName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Mailer constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Swift_Mailer $mailer |
|
39
|
|
|
* @param UrlGeneratorInterface $router |
|
40
|
|
|
* @param EngineInterface $templating |
|
41
|
|
|
* @param array $parameters |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
\Swift_Mailer $mailer, |
|
45
|
|
|
UrlGeneratorInterface $router, |
|
46
|
|
|
EngineInterface $templating, |
|
47
|
|
|
string $fromEmail, |
|
48
|
|
|
string $fromName |
|
49
|
|
|
) { |
|
50
|
|
|
$this->mailer = $mailer; |
|
51
|
|
|
$this->router = $router; |
|
52
|
|
|
$this->templating = $templating; |
|
53
|
|
|
$this->fromEmail = $fromEmail; |
|
54
|
|
|
$this->fromName = $fromName; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $renderedTemplate |
|
59
|
|
|
* @param array|string $fromEmail |
|
60
|
|
|
* @param array|string $toEmail |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function sendEmailMessage($renderedTemplate, $toEmail) |
|
63
|
|
|
{ |
|
64
|
|
|
// Render the email, use the first line as the subject, and the rest as the body |
|
65
|
|
|
$renderedLines = explode("\n", trim($renderedTemplate)); |
|
66
|
|
|
$subject = array_shift($renderedLines); |
|
67
|
|
|
$body = implode("\n", $renderedLines); |
|
68
|
|
|
|
|
69
|
|
|
$message = (new \Swift_Message()) |
|
70
|
|
|
->setSubject($subject) |
|
71
|
|
|
->setFrom($this->fromEmail) |
|
72
|
|
|
->setTo($toEmail) |
|
73
|
|
|
->setBody($body, 'text/html'); |
|
74
|
|
|
|
|
75
|
|
|
$this->mailer->send($message); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|