setEncoding('UTF-8'); } // split value on "," $fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue); $values = explode(',', $fieldValue); array_walk($values, 'trim'); $addressList = $header->getAddressList(); foreach ($values as $address) { // split values into name/email if (!preg_match('/^((?P.*?)<(?P[^>]+)>|(?P.+))$/', $address, $matches)) { // Should we raise an exception here? continue; } $name = null; if (isset($matches['name'])) { $name = trim($matches['name']); } if (empty($name)) { $name = null; } if (isset($matches['namedEmail'])) { $email = $matches['namedEmail']; } if (isset($matches['email'])) { $email = $matches['email']; } $email = trim($email); // we may have leading whitespace // populate address list $addressList->add($email, $name); } return $header; } public function getFieldName() { return $this->fieldName; } public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { $emails = array(); $encoding = $this->getEncoding(); foreach ($this->getAddressList() as $address) { $email = $address->getEmail(); $name = $address->getName(); if (empty($name)) { $emails[] = $email; } else { if (false !== strstr($name, ',')) { $name = sprintf('"%s"', $name); } if ($format == HeaderInterface::FORMAT_ENCODED && 'ASCII' !== $encoding ) { $name = HeaderWrap::mimeEncodeValue($name, $encoding); } $emails[] = sprintf('%s <%s>', $name, $email); } } return implode(',' . Headers::FOLDING, $emails); } public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } public function getEncoding() { return $this->encoding; } /** * Set address list for this header * * @param AddressList $addressList */ public function setAddressList(AddressList $addressList) { $this->addressList = $addressList; } /** * Get address list managed by this header * * @return AddressList */ public function getAddressList() { if (null === $this->addressList) { $this->setAddressList(new AddressList()); } return $this->addressList; } public function toString() { $name = $this->getFieldName(); $value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED); return (empty($value)) ? '' : sprintf('%s: %s', $name, $value); } }