1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Borut Balažek <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Mailer |
11
|
|
|
{ |
12
|
|
|
protected $app; |
13
|
|
|
protected $swiftMessageInstance; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Application $app |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Application $app) |
19
|
|
|
{ |
20
|
|
|
$this->app = $app; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Prepares the (swift) email and sends it. |
25
|
|
|
* |
26
|
|
|
* @param array $data Swiftmailer data |
27
|
|
|
* |
28
|
|
|
* @return int Number of successfully sent emails |
29
|
|
|
* |
30
|
|
|
* @throws \Exception If subject or recipient (to) are not specified |
31
|
|
|
*/ |
32
|
|
|
public function swiftMessageInitializeAndSend(array $data = array()) |
33
|
|
|
{ |
34
|
|
|
$swiftMessageInstance = \Swift_Message::newInstance(); |
35
|
|
|
|
36
|
|
|
if (!isset($data['subject'])) { |
37
|
|
|
throw new \Exception('You need to specify a subject'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!isset($data['to'])) { |
41
|
|
|
throw new \Exception('You need to specify a recipient'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$from = isset($data['from']) |
45
|
|
|
? $data['from'] |
46
|
|
|
: $this->app['email'] |
47
|
|
|
; |
48
|
|
|
$to = $data['to']; |
49
|
|
|
|
50
|
|
|
$swiftMessageInstance |
51
|
|
|
->setSubject($data['subject']) |
52
|
|
|
->setTo($to) |
53
|
|
|
->setFrom($from) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
if (isset($data['cc'])) { |
57
|
|
|
$swiftMessageInstance->setCc($data['cc']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (isset($data['bcc'])) { |
61
|
|
|
$swiftMessageInstance->setBcc($data['bcc']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$templateData = array( |
65
|
|
|
'app' => $this->app, |
66
|
|
|
'user' => $this->app['user'], |
67
|
|
|
'email' => $to, |
68
|
|
|
'swiftMessage' => $swiftMessageInstance, |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if (isset($data['templateData'])) { |
72
|
|
|
$templateData = array_merge( |
73
|
|
|
$templateData, |
74
|
|
|
$data['templateData'] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (isset($data['body'])) { |
79
|
|
|
$bodyType = isset($data['bodyType']) |
80
|
|
|
? $data['bodyType'] |
81
|
|
|
: 'text/html' |
82
|
|
|
; |
83
|
|
|
$isTwigTemplate = isset($data['contentIsTwigTemplate']) |
84
|
|
|
? $data['contentIsTwigTemplate'] |
85
|
|
|
: true |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
$swiftMessageBody = $this->app['mailer.css_to_inline_styles_converter']( |
89
|
|
|
$data['body'], |
90
|
|
|
$templateData, |
91
|
|
|
$isTwigTemplate |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$swiftMessageInstance->setBody($swiftMessageBody, $bodyType); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->app['mailer']->send($swiftMessageInstance); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/***** Swift Message Instance *****/ |
101
|
|
|
/** |
102
|
|
|
* @return \Swift_Message |
103
|
|
|
*/ |
104
|
|
|
public function getSwiftMessageInstance() |
105
|
|
|
{ |
106
|
|
|
return $this->swiftMessageInstance; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Swift_Message $swiftMessageInstance |
111
|
|
|
* |
112
|
|
|
* @return Mailer |
113
|
|
|
*/ |
114
|
|
|
public function setSwiftMessageInstance(\Swift_Message $swiftMessageInstance) |
115
|
|
|
{ |
116
|
|
|
$this->swiftMessageInstance = $swiftMessageInstance; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sends the (swift) email. |
123
|
|
|
* |
124
|
|
|
* @param mixed $swiftMessage |
125
|
|
|
*/ |
126
|
|
|
public function send($swiftMessage = false) |
127
|
|
|
{ |
128
|
|
|
if (!$swiftMessage) { |
129
|
|
|
$swiftMessage = $this->getSwiftMessageInstance(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->app['mailer']->send($swiftMessage); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Short for swift image. |
137
|
|
|
* |
138
|
|
|
* @param string $path |
139
|
|
|
*/ |
140
|
|
|
public function image($path) |
141
|
|
|
{ |
142
|
|
|
return \Swift_Image::fromPath($path); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|