1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhalconExt\Mail; |
4
|
|
|
|
5
|
|
|
use PhalconExt\Logger\LogsToFile; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Mail logger for swift mailer. |
9
|
|
|
* |
10
|
|
|
* @author Jitendra Adhikari <[email protected]> |
11
|
|
|
* @license MIT |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
14
|
|
|
*/ |
15
|
|
|
class Logger implements \Swift_Events_SendListener |
16
|
|
|
{ |
17
|
|
|
use LogsToFile; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $config = []; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $logFormat = "%message%\n\n"; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $fileExtension; |
27
|
|
|
|
28
|
|
|
public function __construct(array $config) |
29
|
|
|
{ |
30
|
|
|
$this->config = $config + ['enabled' => false, 'type' => null]; |
31
|
|
|
|
32
|
|
|
if ($this->check()) { |
33
|
|
|
$this->activate($this->config['logPath']); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if config is fine. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
protected function check(): bool |
43
|
|
|
{ |
44
|
|
|
if (!$this->config['enabled'] || empty($this->config['logPath'])) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->fileExtension = '.' . $this->config['type']; |
49
|
|
|
|
50
|
|
|
if (\in_array($this->config['type'], ['html', 'json', 'eml'])) { |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Right before mail is sent. |
59
|
|
|
* |
60
|
|
|
* @param \Swift_Events_SendEvent $evt |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function beforeSendPerformed(\Swift_Events_SendEvent $evt) |
65
|
|
|
{ |
66
|
|
|
if (!$this->activated) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$message = $this->formatMessage($evt->getMessage()); |
71
|
|
|
|
72
|
|
|
$this->log($message); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Formats message as per type. |
77
|
|
|
* |
78
|
|
|
* @param \Swift_Mime_SimpleMessage $message |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function formatMessage(\Swift_Mime_SimpleMessage $message): string |
83
|
|
|
{ |
84
|
|
|
if ('eml' === $this->config['type']) { |
85
|
|
|
return $message->toString(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parts = $this->getMessageParts($message); |
89
|
|
|
|
90
|
|
|
if ('json' === $this->config['type']) { |
91
|
|
|
return \json_encode($parts); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Html, what else? |
95
|
|
|
return $this->formatHtml(\array_filter($parts)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Format msg parts as html. |
100
|
|
|
* |
101
|
|
|
* @param array $parts |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function formatHtml(array $parts): string |
106
|
|
|
{ |
107
|
|
|
$html = "<div style='text-align: center;'>\n"; |
108
|
|
|
foreach ($parts as $block => $content) { |
109
|
|
|
$html .= "<h3>$block</h3>\n"; |
110
|
|
|
|
111
|
|
|
if (\is_scalar($content)) { |
112
|
|
|
$html .= "<div class='$block'>$content</div>\n"; |
113
|
|
|
|
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ((array) $content as $key => $value) { |
118
|
|
|
$html .= "<p class='$block'>$key: $value</p>\n"; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$html .= '</div>'; |
123
|
|
|
|
124
|
|
|
return $html; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get message parts. |
129
|
|
|
* |
130
|
|
|
* @param \Swift_Mime_SimpleMessage $message |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function getMessageParts(\Swift_Mime_SimpleMessage $message) |
135
|
|
|
{ |
136
|
|
|
$attachments = \array_filter($message->getChildren(), function ($part) { |
137
|
|
|
return $part instanceof \Swift_Attachment; |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
$attachments = \array_map(function ($part) { |
141
|
|
|
return $part->getName(); |
142
|
|
|
}, $attachments); |
143
|
|
|
|
144
|
|
|
return [ |
145
|
|
|
'Subject' => $message->getSubject(), |
146
|
|
|
'From' => $message->getFrom(), |
147
|
|
|
'To' => $message->getTo(), |
148
|
|
|
'Body' => $message->getBody(), |
149
|
|
|
'Attachments' => $attachments, |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Right after mail is sent. |
155
|
|
|
* |
156
|
|
|
* @param \Swift_Events_SendEvent $evt |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function sendPerformed(\Swift_Events_SendEvent $evt) |
161
|
|
|
{ |
162
|
|
|
// Do nothing! |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|