1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkCoreBundle\Mail; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
6
|
|
|
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; |
7
|
|
|
|
8
|
|
|
final class MessageFactory |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array|string |
12
|
|
|
*/ |
13
|
|
|
private $sender = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array|string |
17
|
|
|
*/ |
18
|
|
|
private $replyTo = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array|string |
22
|
|
|
*/ |
23
|
|
|
private $to = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var EngineInterface |
27
|
|
|
*/ |
28
|
|
|
private $template; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $templatePath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $cssPath; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* MessageFactory constructor. |
42
|
|
|
* |
43
|
|
|
* @param EngineInterface $template |
44
|
|
|
* @param string $templatePath |
45
|
|
|
* @param string $cssPath |
46
|
|
|
*/ |
47
|
5 |
|
public function __construct(EngineInterface $template, $templatePath, $cssPath) |
48
|
|
|
{ |
49
|
5 |
|
$this->template = $template; |
50
|
5 |
|
$this->templatePath = $templatePath; |
51
|
5 |
|
$this->cssPath = $cssPath; |
52
|
5 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the default sender |
56
|
|
|
* |
57
|
|
|
* @param string $email |
58
|
|
|
* @param string|null $name |
59
|
|
|
*/ |
60
|
1 |
|
public function setDefaultSender($email, $name = null) |
61
|
|
|
{ |
62
|
1 |
|
if ($name !== null) { |
63
|
1 |
|
$this->sender = [$email => $name]; |
64
|
|
|
} else { |
65
|
|
|
$this->sender = $email; |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the default reply-to |
71
|
|
|
* |
72
|
|
|
* @param string $email |
73
|
|
|
* @param string|null $name |
74
|
|
|
*/ |
75
|
1 |
|
public function setDefaultReplyTo($email, $name = null) |
76
|
|
|
{ |
77
|
1 |
|
if ($name !== null) { |
78
|
1 |
|
$this->replyTo = [$email => $name]; |
79
|
|
|
} else { |
80
|
|
|
$this->replyTo = $email; |
81
|
|
|
} |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the default to |
86
|
|
|
* |
87
|
|
|
* @param string $email |
88
|
|
|
* @param string|null $name |
89
|
|
|
*/ |
90
|
1 |
|
public function setDefaultTo($email, $name = null) |
91
|
|
|
{ |
92
|
1 |
|
if ($name !== null) { |
93
|
1 |
|
$this->to = [$email => $name]; |
94
|
|
|
} else { |
95
|
|
|
$this->to = $email; |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create a message |
101
|
|
|
* |
102
|
|
|
* @param string|null $subject |
103
|
|
|
* @param string|null $html |
104
|
|
|
* @param string|null $alternative |
105
|
|
|
* @return \Swift_Message |
106
|
|
|
*/ |
107
|
2 |
|
private function createMessage($subject = null, $html = null, $alternative = null) |
108
|
|
|
{ |
109
|
2 |
|
$message = $this->createDefaultMessage(); |
110
|
|
|
|
111
|
2 |
|
if ($subject != '') { |
112
|
2 |
|
$message->setSubject($subject); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// only plain text |
116
|
2 |
|
if ($html == '' && $alternative != '') { |
117
|
1 |
|
$message->setBody($alternative, 'text/plain'); |
118
|
|
|
|
119
|
1 |
|
return $message; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// html mail |
123
|
1 |
|
if ($alternative === null) { |
124
|
1 |
|
$alternative = $this->convertToPlainText($html); |
125
|
|
|
} |
126
|
1 |
|
$message->setBody( |
127
|
1 |
|
$this->wrapInTemplate($html), |
128
|
1 |
|
'text/html' |
129
|
|
|
); |
130
|
1 |
|
$message->addPart($alternative, 'text/plain'); |
131
|
|
|
|
132
|
1 |
|
return $message; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Create a HTML message |
137
|
|
|
* |
138
|
|
|
* If no alternative is provided it will be generated automatically. |
139
|
|
|
* This is just an alias for createMessage |
140
|
|
|
* |
141
|
|
|
* @param string|null $subject |
142
|
|
|
* @param string|null $html |
143
|
|
|
* @param string|null $plainText |
144
|
|
|
* @return \Swift_Message |
145
|
|
|
*/ |
146
|
1 |
|
public function createHtmlMessage($subject = null, $html = null, $plainText = null) |
147
|
|
|
{ |
148
|
1 |
|
return $this->createMessage($subject, $html, $plainText); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Create a plain text message |
153
|
|
|
* |
154
|
|
|
* @param string|null $subject |
155
|
|
|
* @param string|null $body |
156
|
|
|
* @return \Swift_Message |
157
|
|
|
*/ |
158
|
1 |
|
public function createPlainTextMessage($subject = null, $body = null) |
159
|
|
|
{ |
160
|
1 |
|
return $this->createMessage($subject, null, $body); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return \Swift_Message |
165
|
|
|
*/ |
166
|
4 |
|
public function createDefaultMessage() |
167
|
|
|
{ |
168
|
4 |
|
$message = \Swift_Message::newInstance(); |
169
|
|
|
|
170
|
4 |
|
if (!empty($this->sender)) { |
171
|
1 |
|
$message->setFrom($this->sender); |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
if (!empty($this->replyTo)) { |
175
|
1 |
|
$message->setReplyTo($this->replyTo); |
176
|
|
|
} |
177
|
|
|
|
178
|
4 |
|
if (!empty($this->to)) { |
179
|
1 |
|
$message->setTo($this->to); |
180
|
|
|
} |
181
|
|
|
|
182
|
4 |
|
return $message; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Wrap the given content in a nice default email template |
187
|
|
|
* |
188
|
|
|
* @param string $content |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
1 |
|
public function wrapInTemplate($content) |
192
|
|
|
{ |
193
|
1 |
|
$css = file_get_contents($this->cssPath); |
194
|
1 |
|
$html = $this->template->render( |
195
|
1 |
|
$this->templatePath, |
196
|
|
|
[ |
197
|
1 |
|
'content' => $content, |
198
|
1 |
|
'css' => $css, |
199
|
|
|
] |
200
|
|
|
); |
201
|
|
|
|
202
|
1 |
|
$cssToInlineStyles = new CssToInlineStyles(); |
203
|
|
|
|
204
|
1 |
|
return $cssToInlineStyles->convert( |
205
|
1 |
|
$html, |
206
|
1 |
|
$css |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Convert the given content from HTML to Plain text |
212
|
|
|
* |
213
|
|
|
* @param string $content |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
2 |
|
public function convertToPlainText($content) |
217
|
|
|
{ |
218
|
2 |
|
$content = preg_replace('/\r\n/', PHP_EOL, $content); |
219
|
2 |
|
$content = preg_replace('/\r/', PHP_EOL, $content); |
220
|
2 |
|
$content = preg_replace("/\t/", '', $content); |
221
|
|
|
|
222
|
|
|
// remove the style- and head-tags and all their contents |
223
|
2 |
|
$content = preg_replace('|\<style.*\>(.*\n*)\</style\>|isU', '', $content); |
224
|
2 |
|
$content = preg_replace('|\<head.*\>(.*\n*)\</head\>|isU', '', $content); |
225
|
|
|
|
226
|
|
|
// replace images with their alternative content |
227
|
2 |
|
$content = preg_replace('|\<img[^>]*alt="(.*)".*/\>|isU', '$1', $content); |
228
|
|
|
|
229
|
|
|
// replace links with the inner html of the link with the url between () |
230
|
2 |
|
$content = preg_replace('|<a.*href="(.*)".*>(.*)</a>|isU', '$2 ($1)', $content); |
231
|
|
|
|
232
|
|
|
// strip HTML tags and preserve paragraphs |
233
|
2 |
|
$content = strip_tags($content, '<p><div>'); |
234
|
|
|
|
235
|
|
|
// remove multiple spaced with a single one |
236
|
2 |
|
$content = preg_replace('/\n\s/', PHP_EOL, $content); |
237
|
2 |
|
$content = preg_replace('/\n{2,}/', PHP_EOL, $content); |
238
|
|
|
|
239
|
|
|
// for each div, paragraph end we want an additional linebreak at the end |
240
|
2 |
|
$content = preg_replace('|<div>|', '', $content); |
241
|
2 |
|
$content = preg_replace('|</div>|', PHP_EOL, $content); |
242
|
2 |
|
$content = preg_replace('|<p>|', '', $content); |
243
|
2 |
|
$content = preg_replace('|</p>|', PHP_EOL, $content); |
244
|
|
|
|
245
|
2 |
|
$content = trim($content); |
246
|
2 |
|
$content = strip_tags($content); |
247
|
2 |
|
$content = html_entity_decode($content); |
248
|
|
|
|
249
|
2 |
|
return $content; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|