Skip to content

Commit becd0b2

Browse files
author
Sebastian Krätzig
committed
Deduplicate code with function for mail header values
1 parent 7e2d978 commit becd0b2

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/PhpImap/Mailbox.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,27 @@ public function getRawMail(int $msgId, bool $markAsSeen = true): string
10611061
return Imap::fetchbody($this->getImapStream(), $msgId, '', $options);
10621062
}
10631063

1064+
/**
1065+
* Get mail header field value.
1066+
*
1067+
* @param string $headersRaw RAW headers as single string
1068+
* @param string $header_field_name Name of the required header field
1069+
*
1070+
* @return string Value of the header field
1071+
*/
1072+
public function getMailHeaderFieldValue(string $headersRaw, string $header_field_name): string
1073+
{
1074+
$header_field_value = '';
1075+
1076+
if (\preg_match("/$header_field_name\:(.*)/i", $headersRaw, $matches)) {
1077+
if (isset($matches[1])) {
1078+
return \trim($matches[1]);
1079+
}
1080+
}
1081+
1082+
return $header_field_value;
1083+
}
1084+
10641085
/**
10651086
* Get mail header.
10661087
*
@@ -1127,20 +1148,20 @@ public function getMailHeader(int $mailId): IncomingMailHeader
11271148
$header->imapPath = $this->imapPath;
11281149
$header->mailboxFolder = $this->mailboxFolder;
11291150
$header->isDraft = (!isset($head->date)) ? true : false;
1130-
$header->mimeVersion = (\preg_match("/MIME-Version\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1131-
$header->xVirusScanned = (\preg_match("/X-Virus-Scanned\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1132-
$header->organization = (\preg_match("/Organization\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1133-
$header->contentType = (\preg_match("/Content-Type\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1134-
$header->xMailer = (\preg_match("/X-Mailer\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1135-
$header->contentLanguage = (\preg_match("/Content-Language\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1136-
$header->xSenderIp = (\preg_match("/X-Sender-IP\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1137-
$header->priority = (\preg_match("/Priority\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1138-
$header->importance = (\preg_match("/Importance\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1139-
$header->sensitivity = (\preg_match("/Sensitivity\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1140-
$header->autoSubmitted = (\preg_match("/Auto-Submitted\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1141-
$header->precedence = (\preg_match("/Precedence\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1142-
$header->failedRecipients = (\preg_match("/Failed-Recipients\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1143-
$header->xOriginalTo = (\preg_match("/X-Original-To\:(.*)/i", $headersRaw, $matches)) ? (!isset($matches[1]) ?: \trim($matches[1])) : '';
1151+
$header->mimeVersion = $this->getMailHeaderFieldValue($headersRaw, 'MIME-Version');
1152+
$header->xVirusScanned = $this->getMailHeaderFieldValue($headersRaw, 'X-Virus-Scanned');
1153+
$header->organization = $this->getMailHeaderFieldValue($headersRaw, 'Organization');
1154+
$header->contentType = $this->getMailHeaderFieldValue($headersRaw, 'Content-Type');
1155+
$header->xMailer = $this->getMailHeaderFieldValue($headersRaw, 'X-Mailer');
1156+
$header->contentLanguage = $this->getMailHeaderFieldValue($headersRaw, 'Content-Language');
1157+
$header->xSenderIp = $this->getMailHeaderFieldValue($headersRaw, 'X-Sender-IP');
1158+
$header->priority = $this->getMailHeaderFieldValue($headersRaw, 'Priority');
1159+
$header->importance = $this->getMailHeaderFieldValue($headersRaw, 'Importance');
1160+
$header->sensitivity = $this->getMailHeaderFieldValue($headersRaw, 'Sensitivity');
1161+
$header->autoSubmitted = $this->getMailHeaderFieldValue($headersRaw, 'Auto-Submitted');
1162+
$header->precedence = $this->getMailHeaderFieldValue($headersRaw, 'Precedence');
1163+
$header->failedRecipients = $this->getMailHeaderFieldValue($headersRaw, 'Failed-Recipients');
1164+
$header->xOriginalTo = $this->getMailHeaderFieldValue($headersRaw, 'X-Original-To');
11441165

11451166
if (isset($head->date) && !empty(\trim($head->date))) {
11461167
$header->date = self::parseDateTime($head->date);

0 commit comments

Comments
 (0)