99
1010use Laminas \Mail \Exception \InvalidArgumentException as LaminasInvalidArgumentException ;
1111use Magento \Framework \App \ObjectManager ;
12- use Magento \Framework \Exception \LocalizedException ;
1312use Magento \Framework \Mail \Exception \InvalidArgumentException ;
1413use Laminas \Mail \Address as LaminasAddress ;
1514use Laminas \Mail \AddressList ;
2524class EmailMessage extends Message implements EmailMessageInterface
2625{
2726 /**
28- * @var LaminasEmailAddress
27+ * @var array.
2928 */
30- private $ emailValidator ;
29+ private const ARRAY_RCE_CHARACTERS = [
30+ ', ' ,
31+ '; ' ,
32+ '< ' ,
33+ '> ' ,
34+ '< ' ,
35+ '> '
36+ ];
3137
3238 /**
3339 * @var MimeMessageInterfaceFactory
@@ -45,7 +51,11 @@ class EmailMessage extends Message implements EmailMessageInterface
4551 private $ logger ;
4652
4753 /**
48- * @param LaminasEmailAddress $emailValidator
54+ * @var LaminasEmailAddress|null
55+ */
56+ private $ emailValidator ;
57+
58+ /**
4959 * @param MimeMessageInterface $body
5060 * @param array $to
5161 * @param MimeMessageInterfaceFactory $mimeMessageFactory
@@ -58,14 +68,13 @@ class EmailMessage extends Message implements EmailMessageInterface
5868 * @param string|null $subject
5969 * @param string|null $encoding
6070 * @param LoggerInterface|null $logger
71+ * @param LaminasEmailAddress|null $emailValidator
6172 * @throws InvalidArgumentException
62- * @throws LocalizedException
6373 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
6474 * @SuppressWarnings(PHPMD.NPathComplexity)
6575 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
6676 */
6777 public function __construct (
68- LaminasEmailAddress $ emailValidator ,
6978 MimeMessageInterface $ body ,
7079 array $ to ,
7180 MimeMessageInterfaceFactory $ mimeMessageFactory ,
@@ -77,12 +86,13 @@ public function __construct(
7786 ?Address $ sender = null ,
7887 ?string $ subject = '' ,
7988 ?string $ encoding = 'utf-8 ' ,
80- ?LoggerInterface $ logger = null
89+ ?LoggerInterface $ logger = null ,
90+ ?LaminasEmailAddress $ emailValidator = null
8191 ) {
8292 parent ::__construct ($ encoding );
83- $ this ->emailValidator = $ emailValidator ;
8493 $ mimeMessage = new LaminasMimeMessage ();
8594 $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
95+ $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (LaminasEmailAddress::class);
8696 $ mimeMessage ->setParts ($ body ->getParts ());
8797 $ this ->zendMessage ->setBody ($ mimeMessage );
8898 if ($ subject ) {
@@ -95,7 +105,7 @@ public function __construct(
95105 );
96106 }
97107 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 ' );
99109 }
100110 if ($ to ) {
101111 $ this ->zendMessage ->setTo ($ this ->convertAddressArrayToAddressList ($ to ));
@@ -135,7 +145,6 @@ public function getHeaders(): array
135145 /**
136146 * @inheritDoc
137147 *
138- * @throws LocalizedException
139148 */
140149 public function getFrom (): ?array
141150 {
@@ -145,7 +154,6 @@ public function getFrom(): ?array
145154 /**
146155 * @inheritDoc
147156 *
148- * @throws LocalizedException
149157 */
150158 public function getTo (): array
151159 {
@@ -155,7 +163,6 @@ public function getTo(): array
155163 /**
156164 * @inheritDoc
157165 *
158- * @throws LocalizedException
159166 */
160167 public function getCc (): ?array
161168 {
@@ -165,7 +172,6 @@ public function getCc(): ?array
165172 /**
166173 * @inheritDoc
167174 *
168- * @throws LocalizedException
169175 */
170176 public function getBcc (): ?array
171177 {
@@ -175,7 +181,6 @@ public function getBcc(): ?array
175181 /**
176182 * @inheritDoc
177183 *
178- * @throws LocalizedException
179184 */
180185 public function getReplyTo (): ?array
181186 {
@@ -185,7 +190,6 @@ public function getReplyTo(): ?array
185190 /**
186191 * @inheritDoc
187192 *
188- * @throws LocalizedException
189193 */
190194 public function getSender (): ?Address
191195 {
@@ -232,7 +236,6 @@ public function toString(): string
232236 *
233237 * @param AddressList $addressList
234238 * @return Address[]
235- * @throws LocalizedException
236239 */
237240 private function convertAddressListToAddressArray (AddressList $ addressList ): array
238241 {
@@ -255,7 +258,7 @@ private function convertAddressListToAddressArray(AddressList $addressList): arr
255258 *
256259 * @param Address[] $arrayList
257260 * @return AddressList
258- * @throws LaminasInvalidArgumentException|LocalizedException
261+ * @throws LaminasInvalidArgumentException
259262 */
260263 private function convertAddressArrayToAddressList (array $ arrayList ): AddressList
261264 {
@@ -283,16 +286,20 @@ private function convertAddressArrayToAddressList(array $arrayList): AddressList
283286 *
284287 * @param ?string $email
285288 * @return ?string
286- * @throws LocalizedException
287289 */
288290 private function sanitiseEmail (?string $ email ): ?string
289291 {
290292 if (isset ($ email ) && str_contains ($ email , '=?? ' )) {
291293 $ 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 )) &&
293296 !$ this ->emailValidator ->isValid ((trim ($ decodedValue )))
294297 ) {
295- throw new LocalizedException (__ ('Sender email must be a valid email address ' ));
298+ $ email = trim (str_replace (
299+ '/ ' ,
300+ '' ,
301+ $ decodedValue
302+ ));
296303 }
297304 }
298305
@@ -309,14 +316,7 @@ private function sanitiseName(?string $name): ?string
309316 {
310317 if (isset ($ name )) {
311318 return trim (str_replace (
312- [
313- ', ' ,
314- '; ' ,
315- '< ' ,
316- '> ' ,
317- '< ' ,
318- '> '
319- ],
319+ self ::ARRAY_RCE_CHARACTERS ,
320320 '' ,
321321 $ name
322322 ));
0 commit comments