99
1010use Laminas \Mail \Exception \InvalidArgumentException as LaminasInvalidArgumentException ;
1111use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \Exception \LocalizedException ;
1213use Magento \Framework \Mail \Exception \InvalidArgumentException ;
1314use Laminas \Mail \Address as LaminasAddress ;
1415use Laminas \Mail \AddressList ;
@@ -51,6 +52,7 @@ class EmailMessage extends Message implements EmailMessageInterface
5152 * @param string|null $encoding
5253 * @param LoggerInterface|null $logger
5354 * @throws InvalidArgumentException
55+ * @throws LocalizedException
5456 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
5557 * @SuppressWarnings(PHPMD.NPathComplexity)
5658 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -124,6 +126,7 @@ public function getHeaders(): array
124126 /**
125127 * @inheritDoc
126128 *
129+ * @throws LocalizedException
127130 */
128131 public function getFrom (): ?array
129132 {
@@ -133,6 +136,7 @@ public function getFrom(): ?array
133136 /**
134137 * @inheritDoc
135138 *
139+ * @throws LocalizedException
136140 */
137141 public function getTo (): array
138142 {
@@ -142,6 +146,7 @@ public function getTo(): array
142146 /**
143147 * @inheritDoc
144148 *
149+ * @throws LocalizedException
145150 */
146151 public function getCc (): ?array
147152 {
@@ -151,6 +156,7 @@ public function getCc(): ?array
151156 /**
152157 * @inheritDoc
153158 *
159+ * @throws LocalizedException
154160 */
155161 public function getBcc (): ?array
156162 {
@@ -160,6 +166,7 @@ public function getBcc(): ?array
160166 /**
161167 * @inheritDoc
162168 *
169+ * @throws LocalizedException
163170 */
164171 public function getReplyTo (): ?array
165172 {
@@ -215,6 +222,7 @@ public function toString(): string
215222 *
216223 * @param AddressList $addressList
217224 * @return Address[]
225+ * @throws LocalizedException
218226 */
219227 private function convertAddressListToAddressArray (AddressList $ addressList ): array
220228 {
@@ -237,7 +245,7 @@ private function convertAddressListToAddressArray(AddressList $addressList): arr
237245 *
238246 * @param Address[] $arrayList
239247 * @return AddressList
240- * @throws LaminasInvalidArgumentException
248+ * @throws LaminasInvalidArgumentException|LocalizedException
241249 */
242250 private function convertAddressArrayToAddressList (array $ arrayList ): AddressList
243251 {
@@ -265,34 +273,24 @@ private function convertAddressArrayToAddressList(array $arrayList): AddressList
265273 *
266274 * @param ?string $email
267275 * @return ?string
276+ * @throws LocalizedException
268277 */
269278 private function sanitiseEmail (?string $ email ): ?string
270279 {
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+ }
278289 }
279290
280291 return $ email ;
281292 }
282293
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-
296294 /**
297295 * Check email contains invalid characters
298296 *
@@ -301,6 +299,7 @@ private function sanitiseEmail(?string $email): ?string
301299 */
302300 private function validateSpecialCharacters (string $ email ): int
303301 {
304- return preg_match ('/^=?.*[#!&%~]+.*$/ ' , $ email );
302+ $ localPart = explode ('@ ' , $ email );
303+ return !empty ($ localPart [0 ]) ? preg_match ('/^.*[#!&%~$+ ]+.*$/ ' , $ localPart [0 ]) : 0 ;
305304 }
306305}
0 commit comments