9
9
10
10
use Laminas \Mail \Exception \InvalidArgumentException as LaminasInvalidArgumentException ;
11
11
use Magento \Framework \App \ObjectManager ;
12
- use Magento \Framework \Exception \LocalizedException ;
13
12
use Magento \Framework \Mail \Exception \InvalidArgumentException ;
14
13
use Laminas \Mail \Address as LaminasAddress ;
15
14
use Laminas \Mail \AddressList ;
25
24
class EmailMessage extends Message implements EmailMessageInterface
26
25
{
27
26
/**
28
- * @var LaminasEmailAddress
27
+ * @var array.
29
28
*/
30
- private $ emailValidator ;
29
+ private const ARRAY_RCE_CHARACTERS = [
30
+ ', ' ,
31
+ '; ' ,
32
+ '< ' ,
33
+ '> ' ,
34
+ '< ' ,
35
+ '> '
36
+ ];
31
37
32
38
/**
33
39
* @var MimeMessageInterfaceFactory
@@ -45,7 +51,11 @@ class EmailMessage extends Message implements EmailMessageInterface
45
51
private $ logger ;
46
52
47
53
/**
48
- * @param LaminasEmailAddress $emailValidator
54
+ * @var LaminasEmailAddress|null
55
+ */
56
+ private $ emailValidator ;
57
+
58
+ /**
49
59
* @param MimeMessageInterface $body
50
60
* @param array $to
51
61
* @param MimeMessageInterfaceFactory $mimeMessageFactory
@@ -58,14 +68,13 @@ class EmailMessage extends Message implements EmailMessageInterface
58
68
* @param string|null $subject
59
69
* @param string|null $encoding
60
70
* @param LoggerInterface|null $logger
71
+ * @param LaminasEmailAddress|null $emailValidator
61
72
* @throws InvalidArgumentException
62
- * @throws LocalizedException
63
73
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
64
74
* @SuppressWarnings(PHPMD.NPathComplexity)
65
75
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
66
76
*/
67
77
public function __construct (
68
- LaminasEmailAddress $ emailValidator ,
69
78
MimeMessageInterface $ body ,
70
79
array $ to ,
71
80
MimeMessageInterfaceFactory $ mimeMessageFactory ,
@@ -77,12 +86,13 @@ public function __construct(
77
86
?Address $ sender = null ,
78
87
?string $ subject = '' ,
79
88
?string $ encoding = 'utf-8 ' ,
80
- ?LoggerInterface $ logger = null
89
+ ?LoggerInterface $ logger = null ,
90
+ ?LaminasEmailAddress $ emailValidator = null
81
91
) {
82
92
parent ::__construct ($ encoding );
83
- $ this ->emailValidator = $ emailValidator ;
84
93
$ mimeMessage = new LaminasMimeMessage ();
85
94
$ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
95
+ $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (LaminasEmailAddress::class);
86
96
$ mimeMessage ->setParts ($ body ->getParts ());
87
97
$ this ->zendMessage ->setBody ($ mimeMessage );
88
98
if ($ subject ) {
@@ -95,7 +105,7 @@ public function __construct(
95
105
);
96
106
}
97
107
if (count ($ to ) < 1 ) {
98
- throw new InvalidArgumentException ('Email message must have at list one addressee ' );
108
+ throw new InvalidArgumentException ('Email message must have at least one addressee ' );
99
109
}
100
110
if ($ to ) {
101
111
$ this ->zendMessage ->setTo ($ this ->convertAddressArrayToAddressList ($ to ));
@@ -135,7 +145,6 @@ public function getHeaders(): array
135
145
/**
136
146
* @inheritDoc
137
147
*
138
- * @throws LocalizedException
139
148
*/
140
149
public function getFrom (): ?array
141
150
{
@@ -145,7 +154,6 @@ public function getFrom(): ?array
145
154
/**
146
155
* @inheritDoc
147
156
*
148
- * @throws LocalizedException
149
157
*/
150
158
public function getTo (): array
151
159
{
@@ -155,7 +163,6 @@ public function getTo(): array
155
163
/**
156
164
* @inheritDoc
157
165
*
158
- * @throws LocalizedException
159
166
*/
160
167
public function getCc (): ?array
161
168
{
@@ -165,7 +172,6 @@ public function getCc(): ?array
165
172
/**
166
173
* @inheritDoc
167
174
*
168
- * @throws LocalizedException
169
175
*/
170
176
public function getBcc (): ?array
171
177
{
@@ -175,7 +181,6 @@ public function getBcc(): ?array
175
181
/**
176
182
* @inheritDoc
177
183
*
178
- * @throws LocalizedException
179
184
*/
180
185
public function getReplyTo (): ?array
181
186
{
@@ -185,7 +190,6 @@ public function getReplyTo(): ?array
185
190
/**
186
191
* @inheritDoc
187
192
*
188
- * @throws LocalizedException
189
193
*/
190
194
public function getSender (): ?Address
191
195
{
@@ -232,7 +236,6 @@ public function toString(): string
232
236
*
233
237
* @param AddressList $addressList
234
238
* @return Address[]
235
- * @throws LocalizedException
236
239
*/
237
240
private function convertAddressListToAddressArray (AddressList $ addressList ): array
238
241
{
@@ -255,7 +258,7 @@ private function convertAddressListToAddressArray(AddressList $addressList): arr
255
258
*
256
259
* @param Address[] $arrayList
257
260
* @return AddressList
258
- * @throws LaminasInvalidArgumentException|LocalizedException
261
+ * @throws LaminasInvalidArgumentException
259
262
*/
260
263
private function convertAddressArrayToAddressList (array $ arrayList ): AddressList
261
264
{
@@ -283,16 +286,20 @@ private function convertAddressArrayToAddressList(array $arrayList): AddressList
283
286
*
284
287
* @param ?string $email
285
288
* @return ?string
286
- * @throws LocalizedException
287
289
*/
288
290
private function sanitiseEmail (?string $ email ): ?string
289
291
{
290
292
if (isset ($ email ) && str_contains ($ email , '=?? ' )) {
291
293
$ decodedValue = iconv_mime_decode ($ email , ICONV_MIME_DECODE_CONTINUE_ON_ERROR , 'UTF-8 ' );
292
- if ($ this ->isEncoded (trim ($ email ), trim ($ decodedValue )) &&
294
+ if (
295
+ $ this ->isEncoded (trim ($ email ), trim ($ decodedValue )) &&
293
296
!$ this ->emailValidator ->isValid ((trim ($ decodedValue )))
294
297
) {
295
- throw new LocalizedException (__ ('Sender email must be a valid email address ' ));
298
+ $ email = trim (str_replace (
299
+ '/ ' ,
300
+ '' ,
301
+ $ decodedValue
302
+ ));
296
303
}
297
304
}
298
305
@@ -309,14 +316,7 @@ private function sanitiseName(?string $name): ?string
309
316
{
310
317
if (isset ($ name )) {
311
318
return trim (str_replace (
312
- [
313
- ', ' ,
314
- '; ' ,
315
- '< ' ,
316
- '> ' ,
317
- '< ' ,
318
- '> '
319
- ],
319
+ self ::ARRAY_RCE_CHARACTERS ,
320
320
'' ,
321
321
$ name
322
322
));
0 commit comments