3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
8 namespace Zend\Mail\Header
;
14 * Sender header class methods.
16 * @see https://tools.ietf.org/html/rfc2822 RFC 2822
17 * @see https://tools.ietf.org/html/rfc2047 RFC 2047
19 class Sender
implements HeaderInterface
22 * @var \Zend\Mail\Address\AddressInterface
33 public static function fromString($headerLine)
35 list($name, $value) = GenericHeader
::splitHeaderLine($headerLine);
36 $value = HeaderWrap
::mimeDecodeValue($value);
38 // check to ensure proper header type for this factory
39 if (strtolower($name) !== 'sender') {
40 throw new Exception\
InvalidArgumentException('Invalid header name for Sender string');
43 $header = new static();
46 * matches the header value so that the email must be enclosed by < > when a name is present
47 * 'name' and 'email' capture groups correspond respectively to 'display-name' and 'addr-spec' in the ABNF
48 * @see https://tools.ietf.org/html/rfc5322#section-3.4
50 $hasMatches = preg_match(
51 '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
56 if ($hasMatches !== 1) {
57 throw new Exception\
InvalidArgumentException('Invalid header value for Sender string');
60 $senderName = trim($matches['name']);
62 if (empty($senderName)) {
66 $header->setAddress($matches['email'], $senderName);
71 public function getFieldName()
76 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
78 if (! $this->address
instanceof Mail\Address\AddressInterface
) {
82 $email = sprintf('<%s>', $this->address
->getEmail());
83 $name = $this->address
->getName();
86 if ($format == HeaderInterface
::FORMAT_ENCODED
) {
87 $encoding = $this->getEncoding();
88 if ('ASCII' !== $encoding) {
89 $name = HeaderWrap
::mimeEncodeValue($name, $encoding);
92 $email = sprintf('%s %s', $name, $email);
98 public function setEncoding($encoding)
100 $this->encoding
= $encoding;
104 public function getEncoding()
106 if (! $this->encoding
) {
107 $this->encoding
= Mime
::isPrintable($this->getFieldValue(HeaderInterface
::FORMAT_RAW
))
112 return $this->encoding
;
115 public function toString()
117 return 'Sender: ' . $this->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
121 * Set the address used in this header
123 * @param string|\Zend\Mail\Address\AddressInterface $emailOrAddress
124 * @param null|string $name
125 * @throws Exception\InvalidArgumentException
128 public function setAddress($emailOrAddress, $name = null)
130 if (is_string($emailOrAddress)) {
131 $emailOrAddress = new Mail\
Address($emailOrAddress, $name);
132 } elseif (! $emailOrAddress instanceof Mail\Address\AddressInterface
) {
133 throw new Exception\
InvalidArgumentException(sprintf(
134 '%s expects a string or AddressInterface object; received "%s"',
136 (is_object($emailOrAddress) ?
get_class($emailOrAddress) : gettype($emailOrAddress))
139 $this->address
= $emailOrAddress;
144 * Retrieve the internal address from this header
146 * @return \Zend\Mail\Address\AddressInterface|null
148 public function getAddress()
150 return $this->address
;