9
9
10
10
use Laminas \Mail \Exception \InvalidArgumentException as LaminasInvalidArgumentException ;
11
11
use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \Framework \Mail \Exception \InvalidArgumentException ;
13
14
use Laminas \Mail \Address as LaminasAddress ;
14
15
use Laminas \Mail \AddressList ;
@@ -51,6 +52,7 @@ class EmailMessage extends Message implements EmailMessageInterface
51
52
* @param string|null $encoding
52
53
* @param LoggerInterface|null $logger
53
54
* @throws InvalidArgumentException
55
+ * @throws LocalizedException
54
56
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
55
57
* @SuppressWarnings(PHPMD.NPathComplexity)
56
58
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -124,6 +126,7 @@ public function getHeaders(): array
124
126
/**
125
127
* @inheritDoc
126
128
*
129
+ * @throws LocalizedException
127
130
*/
128
131
public function getFrom (): ?array
129
132
{
@@ -133,6 +136,7 @@ public function getFrom(): ?array
133
136
/**
134
137
* @inheritDoc
135
138
*
139
+ * @throws LocalizedException
136
140
*/
137
141
public function getTo (): array
138
142
{
@@ -142,6 +146,7 @@ public function getTo(): array
142
146
/**
143
147
* @inheritDoc
144
148
*
149
+ * @throws LocalizedException
145
150
*/
146
151
public function getCc (): ?array
147
152
{
@@ -151,6 +156,7 @@ public function getCc(): ?array
151
156
/**
152
157
* @inheritDoc
153
158
*
159
+ * @throws LocalizedException
154
160
*/
155
161
public function getBcc (): ?array
156
162
{
@@ -160,6 +166,7 @@ public function getBcc(): ?array
160
166
/**
161
167
* @inheritDoc
162
168
*
169
+ * @throws LocalizedException
163
170
*/
164
171
public function getReplyTo (): ?array
165
172
{
@@ -215,6 +222,7 @@ public function toString(): string
215
222
*
216
223
* @param AddressList $addressList
217
224
* @return Address[]
225
+ * @throws LocalizedException
218
226
*/
219
227
private function convertAddressListToAddressArray (AddressList $ addressList ): array
220
228
{
@@ -237,7 +245,7 @@ private function convertAddressListToAddressArray(AddressList $addressList): arr
237
245
*
238
246
* @param Address[] $arrayList
239
247
* @return AddressList
240
- * @throws LaminasInvalidArgumentException
248
+ * @throws LaminasInvalidArgumentException|LocalizedException
241
249
*/
242
250
private function convertAddressArrayToAddressList (array $ arrayList ): AddressList
243
251
{
@@ -265,34 +273,24 @@ private function convertAddressArrayToAddressList(array $arrayList): AddressList
265
273
*
266
274
* @param ?string $email
267
275
* @return ?string
276
+ * @throws LocalizedException
268
277
*/
269
278
private function sanitiseEmail (?string $ email ): ?string
270
279
{
271
- if (!empty ($ email ) && str_starts_with ($ email , '=? ' )) {
272
- return null ;
273
- //$decodedValue = iconv_mime_decode($email, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
274
- // To avoid special characters inside email
275
- // if ($this->validateSpecialCharacters($email)) {
276
- // $email = null;
277
- // }
280
+ if (!empty ($ email )) {
281
+ $ decodedValue = iconv_mime_decode ($ email , ICONV_MIME_DECODE_CONTINUE_ON_ERROR , 'UTF-8 ' );
282
+ $ localPart = explode ('@ ' , $ decodedValue );
283
+ if (!empty ($ localPart [0 ]) && str_starts_with ($ email , '=? ' ) && str_contains ($ localPart [0 ], ' ' )) {
284
+ throw new LocalizedException (__ ('Invalid email format ' ));
285
+ }
286
+ if ($ this ->validateSpecialCharacters ($ email )) {
287
+ throw new LocalizedException (__ ('Invalid email format ' ));
288
+ }
278
289
}
279
290
280
291
return $ email ;
281
292
}
282
293
283
- // /**
284
- // * Check email is encoded
285
- // *
286
- // * @param string $originalEmail
287
- // * @param string $decodedEmail
288
- // * @return bool
289
- // */
290
- // private function isEncoded(string $originalEmail, string $decodedEmail): bool
291
- // {
292
- // return str_starts_with($originalEmail, '=?')
293
- // && strlen($originalEmail) !== strlen($decodedEmail);
294
- // }
295
-
296
294
/**
297
295
* Check email contains invalid characters
298
296
*
@@ -301,6 +299,7 @@ private function sanitiseEmail(?string $email): ?string
301
299
*/
302
300
private function validateSpecialCharacters (string $ email ): int
303
301
{
304
- return preg_match ('/^=?.*[#!&%~]+.*$/ ' , $ email );
302
+ $ localPart = explode ('@ ' , $ email );
303
+ return !empty ($ localPart [0 ]) ? preg_match ('/^.*[#!&%~$+ ]+.*$/ ' , $ localPart [0 ]) : 0 ;
305
304
}
306
305
}
0 commit comments