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