1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bummzack\SwiftMailer\EmogrifyPlugin; |
4
|
|
|
|
5
|
|
|
use Pelago\Emogrifier; |
6
|
|
|
use Swift_Events_SendEvent; |
7
|
|
|
use Swift_Events_SendListener; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Emogrifier Plugin that will convert your CSS to inline styles. |
11
|
|
|
* To be used with SwiftMailer. |
12
|
|
|
* |
13
|
|
|
* @package Bummzack\SwiftMailer\EmogrifyPlugin |
14
|
|
|
*/ |
15
|
|
|
class EmogrifierPlugin implements Swift_Events_SendListener |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Emogrifier |
19
|
|
|
*/ |
20
|
|
|
protected $emogrifier; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Emogrifier $emogrifier |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Emogrifier $emogrifier = null) |
26
|
|
|
{ |
27
|
|
|
if ($emogrifier) { |
28
|
|
|
$this->emogrifier = $emogrifier; |
29
|
|
|
} else { |
30
|
|
|
$this->emogrifier = new Emogrifier(); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Access to the emogrifier instance that's being used internally |
36
|
|
|
* @return Emogrifier |
37
|
|
|
*/ |
38
|
|
|
public function getEmogrifier() |
39
|
|
|
{ |
40
|
|
|
return $this->emogrifier; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the emogrifier instance that's being used internally |
45
|
|
|
* @param Emogrifier $emogrifier |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setEmogrifier(Emogrifier $emogrifier) |
49
|
|
|
{ |
50
|
|
|
$this->emogrifier = $emogrifier; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Swift_Events_SendEvent $event |
56
|
|
|
*/ |
57
|
|
|
public function beforeSendPerformed(Swift_Events_SendEvent $event) |
58
|
|
|
{ |
59
|
|
|
$message = $event->getMessage(); |
60
|
|
|
|
61
|
|
|
$body = $message->getBody(); |
62
|
|
|
if (!empty($body) && $message->getContentType() !== 'text/plain') { |
63
|
|
|
$this->emogrifier->setHtml($body); |
64
|
|
|
$message->setBody($this->emogrifier->emogrify()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($message->getChildren() as $messagePart) { |
68
|
|
|
if ($messagePart->getContentType() === 'text/html') { |
69
|
|
|
$body = $messagePart->getBody(); |
70
|
|
|
|
71
|
|
|
if (empty($body)) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->emogrifier->setHtml($body); |
76
|
|
|
$messagePart->setBody($this->emogrifier->emogrify()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Swift_Events_SendEvent $event |
83
|
|
|
*/ |
84
|
|
|
public function sendPerformed(\Swift_Events_SendEvent $event) |
85
|
|
|
{ |
86
|
|
|
/* No op */ |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|