Skip to content

Commit 0eb7d89

Browse files
authored
fix: extract returnPath as mail address from header (#106)
1 parent e9f5ba6 commit 0eb7d89

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/Controller/Api/MailArchiveController.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ private function enrichFromEml(string $emlPath, Email $email): void
211211
continue;
212212
}
213213

214-
// Extract first item for return-path since Symfony/Mailer needs to be a string value here
215-
if ($header->getName() === 'Return-Path' && is_array($headerValue)) {
216-
$headerValue = array_pop($headerValue);
214+
if ($header->getName() === 'Return-Path') {
215+
$headerValue = $this->determineReturnPath($headerValue);
216+
217+
if ($headerValue === null) {
218+
continue;
219+
}
217220
}
218221

219222
$email->getHeaders()->addHeader($header->getName(), $headerValue);
@@ -285,4 +288,39 @@ private function getContent(?string $emlPath): false|string
285288

286289
return $this->emlFileManager->getEmlFileAsString($emlPath);
287290
}
291+
292+
/**
293+
* @param \DateTimeImmutable|array<Address>|string|null $headerValue
294+
*/
295+
private function determineReturnPath(\DateTimeImmutable|array|string|null $headerValue): ?string
296+
{
297+
// Extract first item for return-path since Symfony/Mailer needs to be a string value here
298+
if (is_array($headerValue)) {
299+
$headerValue = array_pop($headerValue);
300+
}
301+
302+
// extract mail from: <"[email protected]" <[email protected]>>
303+
// see https://github.com/symfony/symfony/pull/59796
304+
if ($headerValue instanceof Address) {
305+
return $headerValue->getEncodedAddress();
306+
}
307+
308+
if (is_string($headerValue)) {
309+
$regex = '/[<"]([^<>"\s]+@[^<>"\s]+)[>"]/';
310+
preg_match($regex, $headerValue, $matches);
311+
if (isset($matches[1])) {
312+
$headerValue = $matches[1];
313+
}
314+
}
315+
316+
if (is_string($headerValue)) {
317+
try {
318+
return (new Address($headerValue))->getEncodedAddress();
319+
} catch (\Throwable) {
320+
// we don't care about invalid addresses
321+
}
322+
}
323+
324+
return null;
325+
}
288326
}

0 commit comments

Comments
 (0)