|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ____ _ _ _ _ |
|
5
|
|
|
* | _ \| |__ | |_ ___ _ __ ___ __ _(_) | |
|
6
|
|
|
* | |_) | '_ \| __/ _ \ '_ ` _ \ / _` | | | |
|
7
|
|
|
* | __/| | | | || __/ | | | | | (_| | | | |
|
8
|
|
|
* |_| |_| |_|\__\___|_| |_| |_|\__,_|_|_| |
|
9
|
|
|
* |
|
10
|
|
|
* This file is part of Kristuff\Phtemail. |
|
11
|
|
|
* |
|
12
|
|
|
* (c) Kristuff <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
* |
|
17
|
|
|
* @version 0.2.0 |
|
18
|
|
|
* @copyright 2017-2020 Kristuff |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Kristuff\Phtemail\Core; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Base class for HtmlEmailBuilder |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class HtmlBuilder |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Align constants |
|
31
|
|
|
* |
|
32
|
|
|
* @access public |
|
33
|
|
|
*/ |
|
34
|
|
|
public const H_ALIGN_LEFT = 'left'; |
|
35
|
|
|
public const H_ALIGN_RIGHT = 'right'; |
|
36
|
|
|
public const H_ALIGN_CENTER = 'center'; |
|
37
|
|
|
public const V_ALIGN_TOP = 'top'; |
|
38
|
|
|
public const V_ALIGN_BOTTOM = 'bottom'; |
|
39
|
|
|
public const V_ALIGN_CENTER = 'middle'; |
|
40
|
|
|
/** |
|
41
|
|
|
* Colors constants todo |
|
42
|
|
|
* |
|
43
|
|
|
* @access public |
|
44
|
|
|
*/ |
|
45
|
|
|
public const COLOR_WHITE = '#FFFFFF' ; |
|
46
|
|
|
public const COLOR_BLACK = '#000000' ; |
|
47
|
|
|
public const COLOR_BLUE = '#2E7BA2' ; |
|
48
|
|
|
public const COLOR_GREEN = '#26A85C' ; |
|
49
|
|
|
public const COLOR_ORANGE = '#f26522' ; |
|
50
|
|
|
public const COLOR_RED = '#c21a1a' ; |
|
51
|
|
|
public const COLOR_YELLOW = '#e4c515' ; |
|
52
|
|
|
public const COLOR_MAGENTA = '#b64aa9' ; |
|
53
|
|
|
|
|
54
|
|
|
public const COLOR_LIGHTGRAY = '#cccccc'; |
|
55
|
|
|
public const COLOR_DARKGRAY = '#1E1F22'; |
|
56
|
|
|
|
|
57
|
|
|
public const COLOR_GRAY_100 = '#eeeeee'; |
|
58
|
|
|
public const COLOR_GRAY_200 = '#d7d7dd'; |
|
59
|
|
|
public const COLOR_GRAY_300 = '#b3b4b9'; |
|
60
|
|
|
public const COLOR_GRAY_400 = '#898a94'; |
|
61
|
|
|
public const COLOR_GRAY_500 = '#6b6f75'; |
|
62
|
|
|
public const COLOR_GRAY_600 = '#56565c'; |
|
63
|
|
|
public const COLOR_GRAY_700 = '#353538'; |
|
64
|
|
|
public const COLOR_GRAY_800 = '#1e1f22'; |
|
65
|
|
|
public const COLOR_GRAY_900 = '#141416'; |
|
66
|
|
|
|
|
67
|
|
|
public const COLOR_STATUS_ERROR = '#c41818' ; |
|
68
|
|
|
public const COLOR_STATUS_SUCCESS = '#118a39' ; |
|
69
|
|
|
public const COLOR_STATUS_WARNING = '#e79c11' ; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets whether html comments are rendered when calling the calling the HtmlEmailBuilder::getHtml() method. |
|
73
|
|
|
* Default is false. |
|
74
|
|
|
* |
|
75
|
|
|
* @access protected |
|
76
|
|
|
* @var bool $renderHtmlComments |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $renderHtmlComments = false; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets whether css comments are rendered when calling the calling the HtmlEmailBuilder::getHtml() method. |
|
82
|
|
|
* Default is false. |
|
83
|
|
|
* |
|
84
|
|
|
* @access protected |
|
85
|
|
|
* @var bool $renderCssComments |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $renderCssComments = false; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Defines the email 'body' width in pixels |
|
91
|
|
|
* Default is 600 |
|
92
|
|
|
* |
|
93
|
|
|
* @access protected |
|
94
|
|
|
* @var int $emailBodyWidth |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $emailBodyWidth = 600; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Defines the background color for backside part, outside the email body |
|
100
|
|
|
* |
|
101
|
|
|
* @access protected |
|
102
|
|
|
* @var string $backsideBackgroundColor |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $backsideBackgroundColor = self::COLOR_GRAY_100; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Defines the color for backside part, outside the email body |
|
108
|
|
|
* |
|
109
|
|
|
* @access protected |
|
110
|
|
|
* @var string $backsideColor |
|
111
|
|
|
*/ |
|
112
|
|
|
protected $backsideColor = self::COLOR_GRAY_600; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Defines the main text color for the email body |
|
116
|
|
|
* |
|
117
|
|
|
* @access protected |
|
118
|
|
|
* @var string $emailBodyColor |
|
119
|
|
|
*/ |
|
120
|
|
|
protected $emailBodyColor = self::COLOR_GRAY_600; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Defines the main text color for the email body |
|
124
|
|
|
* |
|
125
|
|
|
* @access protected |
|
126
|
|
|
* @var string $emailBodyColor |
|
127
|
|
|
*/ |
|
128
|
|
|
protected $emailheaddingColor = self::COLOR_GRAY_900; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Defines the email body background color |
|
132
|
|
|
* Default is #FFFFFF white |
|
133
|
|
|
* |
|
134
|
|
|
* @access protected |
|
135
|
|
|
* @var string $emailBodyBackgroundColor |
|
136
|
|
|
*/ |
|
137
|
|
|
protected $emailBodyBackgroundColor = self::COLOR_WHITE; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Defines the primary fonts for our html email |
|
141
|
|
|
* Default is system fonts |
|
142
|
|
|
* |
|
143
|
|
|
* @access protected |
|
144
|
|
|
* @var string $fonts |
|
145
|
|
|
*/ |
|
146
|
|
|
protected $emailBodyFont = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"; |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Define the 'body' font size in pixels |
|
150
|
|
|
* Default is 15 |
|
151
|
|
|
* |
|
152
|
|
|
* @access protected |
|
153
|
|
|
* @var int $emailBodyFontSize |
|
154
|
|
|
*/ |
|
155
|
|
|
protected $emailBodyFontSize = 15; |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Defines the 'body' line height |
|
159
|
|
|
* Default is 1.6 |
|
160
|
|
|
* |
|
161
|
|
|
* @access protected |
|
162
|
|
|
* @var float $lineHeight |
|
163
|
|
|
*/ |
|
164
|
|
|
protected $lineHeight = 1.6; |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Validates whether a hexadecimal color value is syntactically correct. |
|
168
|
|
|
* The hexadecimal string to validate. May contain a leading '#'. We remove it as |
|
169
|
|
|
* our lib will put it. May use the shorthand notation (e.g., '123' for '112233'). |
|
170
|
|
|
* |
|
171
|
|
|
* @access public |
|
172
|
|
|
* @static |
|
173
|
|
|
* @param string $color The hexadecimal string to validate |
|
174
|
|
|
* |
|
175
|
|
|
* @return string A valid hex color without a leading '#' |
|
176
|
|
|
* @throws \InvalidArgumentException if the color is not a valid hex color |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function validateColor(string $color) |
|
179
|
|
|
{ |
|
180
|
|
|
// Hash prefix is optional. |
|
181
|
|
|
$color = ltrim($color, '#'); |
|
182
|
|
|
|
|
183
|
|
|
// Must be either RGB or RRGGBB. |
|
184
|
|
|
// Must be a valid hex value. |
|
185
|
|
|
$length = mb_strlen($color); |
|
186
|
|
|
|
|
187
|
|
|
if ( ($length === 3 || $length === 6 ) && ctype_xdigit($color) ) { |
|
188
|
|
|
// return prefixed uppercase hex color string |
|
189
|
|
|
return '#'. strtoupper($color); |
|
190
|
|
|
}; |
|
191
|
|
|
|
|
192
|
|
|
// something was wrong |
|
193
|
|
|
throw new \InvalidArgumentException('Value passed was not a valid hex color. Value was [' . $color .']'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* ************************************************ |
|
198
|
|
|
* **************** Public methods **************** |
|
199
|
|
|
* ************************************************ |
|
200
|
|
|
*/ |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets whether html comments are rendered when calling the HtmlEmailBuilder::getHtml() method. |
|
204
|
|
|
* Default is false. This property must be set before calling the HtmlEmailBuilder::getHtml() method. |
|
205
|
|
|
* |
|
206
|
|
|
* $builder = new HtmlEmailBuilder(); |
|
207
|
|
|
* ... |
|
208
|
|
|
* $builder->renderHtmlComments(true); |
|
209
|
|
|
* $htmlContent = $builder->getHtml(); |
|
210
|
|
|
* |
|
211
|
|
|
* @access public |
|
212
|
|
|
* @param bool $value True or false to render or not comments |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public function renderHtmlComments(bool $value) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->renderHtmlComments= $value; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Sets whether css comments are rendered when calling the HtmlEmailBuilder::getHtml() method. |
|
223
|
|
|
* Default is false. This property must be set before calling the HtmlEmailBuilder::getHtml() method. |
|
224
|
|
|
* |
|
225
|
|
|
* $builder = new HtmlEmailBuilder(); |
|
226
|
|
|
* ... |
|
227
|
|
|
* $builder->renderCssComments(true); |
|
228
|
|
|
* $htmlContent = $builder->getHtml(); |
|
229
|
|
|
* |
|
230
|
|
|
* @access public |
|
231
|
|
|
* @param bool $value True or false to render or not comments |
|
232
|
|
|
* |
|
233
|
|
|
* @return void |
|
234
|
|
|
*/ |
|
235
|
|
|
public function renderCssComments(bool $value) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->renderCssComments = $value; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get a formatted html comment for given string if $renderHtmlComments is true, |
|
242
|
|
|
* otherwise an empty string |
|
243
|
|
|
* |
|
244
|
|
|
* @access public |
|
245
|
|
|
* @param string $text The comment text |
|
246
|
|
|
* @param string $indent The indentation before comment. Default is empty string |
|
247
|
|
|
* |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getHtmlComment(string $text, string $indent = '') |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->renderHtmlComments ? $indent . '<!-- ' . $text . ' -->' . PHP_EOL : ''; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Sets the backside background color. |
|
257
|
|
|
* |
|
258
|
|
|
* @access public |
|
259
|
|
|
* @param string $value The hex color string |
|
260
|
|
|
* |
|
261
|
|
|
* @return void |
|
262
|
|
|
* @throws \InvalidArgumentException if the color is not a valid hex color |
|
263
|
|
|
*/ |
|
264
|
|
|
public function setBacksideBackgroundColor(string $value) |
|
265
|
|
|
{ |
|
266
|
|
|
$this->backsideBackgroundColor = self::validateColor($value); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Gets the backside background color. |
|
271
|
|
|
* |
|
272
|
|
|
* @access public |
|
273
|
|
|
* |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
|
public function backsideBackgroundColor() |
|
277
|
|
|
{ |
|
278
|
|
|
return $this->backsideBackgroundColor; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Sets the backside color. |
|
283
|
|
|
* |
|
284
|
|
|
* @access public |
|
285
|
|
|
* @param string $value The hex color string |
|
286
|
|
|
* |
|
287
|
|
|
* @return void |
|
288
|
|
|
* @throws \InvalidArgumentException if the color is not a valid hex color |
|
289
|
|
|
*/ |
|
290
|
|
|
public function setBacksideColor(string $value) |
|
291
|
|
|
{ |
|
292
|
|
|
$this->backsideColor = self::validateColor($value); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Gets the backside color. |
|
297
|
|
|
* |
|
298
|
|
|
* @access public |
|
299
|
|
|
* |
|
300
|
|
|
* @return string |
|
301
|
|
|
*/ |
|
302
|
|
|
public function backsideColor() |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->backsideColor; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Gets the email body width in pixels |
|
309
|
|
|
* |
|
310
|
|
|
* @access public |
|
311
|
|
|
* @return int |
|
312
|
|
|
*/ |
|
313
|
|
|
public function emailBodyWidth() |
|
314
|
|
|
{ |
|
315
|
|
|
return $this->emailBodyWidth; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Sets the email body width. |
|
320
|
|
|
* |
|
321
|
|
|
* @access public |
|
322
|
|
|
* @param int $value The email body width |
|
323
|
|
|
* |
|
324
|
|
|
* @return void |
|
325
|
|
|
*/ |
|
326
|
|
|
public function setEmailBodyWidth(int $value) |
|
327
|
|
|
{ |
|
328
|
|
|
//todo validate |
|
329
|
|
|
$this->emailBodyWidth = $value; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Gets the email body background color. |
|
334
|
|
|
* |
|
335
|
|
|
* @access public |
|
336
|
|
|
* @return string |
|
337
|
|
|
*/ |
|
338
|
|
|
public function emailBodyBackground() |
|
339
|
|
|
{ |
|
340
|
|
|
return $this->emailBodyBackgroundColor; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Sets the 'body' background color. |
|
345
|
|
|
* |
|
346
|
|
|
* @access public |
|
347
|
|
|
* @param string $value The body color |
|
348
|
|
|
* |
|
349
|
|
|
* @return void |
|
350
|
|
|
* @throws \InvalidArgumentException if the color is not a valid hex color |
|
351
|
|
|
*/ |
|
352
|
|
|
public function setEmailBodyBackgroundColor(string $value) |
|
353
|
|
|
{ |
|
354
|
|
|
$this->emailBodyBackgroundColor = self::validateColor($value); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Sets the email body font |
|
359
|
|
|
* |
|
360
|
|
|
* @access public |
|
361
|
|
|
* |
|
362
|
|
|
* @return string |
|
363
|
|
|
*/ |
|
364
|
|
|
public function emailBodyFont() |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->emailBodyFont; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Sets the email body font family |
|
371
|
|
|
* |
|
372
|
|
|
* @access public |
|
373
|
|
|
* @param string $value The body font family |
|
374
|
|
|
* |
|
375
|
|
|
* @return void |
|
376
|
|
|
*/ |
|
377
|
|
|
public function setEmailBodyFont(string $value) |
|
378
|
|
|
{ |
|
379
|
|
|
$this->emailBodyFont = $value; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Gets the email body font size |
|
384
|
|
|
* |
|
385
|
|
|
* @access public |
|
386
|
|
|
* @return string |
|
387
|
|
|
*/ |
|
388
|
|
|
public function emailBodyFontSize() |
|
389
|
|
|
{ |
|
390
|
|
|
return $this->emailBodyFontSize .'px'; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Sets the email body font size in pixels |
|
395
|
|
|
* |
|
396
|
|
|
* @access public |
|
397
|
|
|
* @param int $value The body font size in pixels |
|
398
|
|
|
* |
|
399
|
|
|
* @return string |
|
400
|
|
|
*/ |
|
401
|
|
|
public function setEmailBodyFontSize(int $value) |
|
402
|
|
|
{ |
|
403
|
|
|
$this->emailBodyFontSize = $value; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Gets the email body font |
|
408
|
|
|
* |
|
409
|
|
|
* @access public |
|
410
|
|
|
* @return string |
|
411
|
|
|
*/ |
|
412
|
|
|
public function emailBodyColor() |
|
413
|
|
|
{ |
|
414
|
|
|
return $this->emailBodyColor; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Sets the email body text color. |
|
419
|
|
|
* |
|
420
|
|
|
* @access public |
|
421
|
|
|
* @param string $value The body text color |
|
422
|
|
|
* |
|
423
|
|
|
* @return void |
|
424
|
|
|
* @throws \InvalidArgumentException if the color is not a valid hex color |
|
425
|
|
|
*/ |
|
426
|
|
|
public function setEmailBodyColor(string $value) |
|
427
|
|
|
{ |
|
428
|
|
|
$this->emailBodyColor = self::validateColor($value); |
|
429
|
|
|
} |
|
430
|
|
|
} |