3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Header
;
16 * Sender header class methods.
18 * @see https://tools.ietf.org/html/rfc2822 RFC 2822
19 * @see https://tools.ietf.org/html/rfc2047 RFC 2047
21 class Sender
implements HeaderInterface
24 * @var \Zend\Mail\Address\AddressInterface
35 public static function fromString($headerLine)
37 list($name, $value) = GenericHeader
::splitHeaderLine($headerLine);
38 $value = HeaderWrap
::mimeDecodeValue($value);
40 // check to ensure proper header type for this factory
41 if (strtolower($name) !== 'sender') {
42 throw new Exception\
InvalidArgumentException('Invalid header line for Sender string');
45 $header = new static();
49 // Check for address, and set if found
50 if (preg_match('/^(?P<name>.*?)<(?P<email>[^>]+)>$/', $value, $matches)) {
51 $senderName = trim($matches['name']);
52 if (empty($senderName)) {
55 $senderEmail = $matches['email'];
57 $senderEmail = $value;
60 $header->setAddress($senderEmail, $senderName);
65 public function getFieldName()
70 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
72 if (! $this->address
instanceof Mail\Address\AddressInterface
) {
76 $email = sprintf('<%s>', $this->address
->getEmail());
77 $name = $this->address
->getName();
80 if ($format == HeaderInterface
::FORMAT_ENCODED
) {
81 $encoding = $this->getEncoding();
82 if ('ASCII' !== $encoding) {
83 $name = HeaderWrap
::mimeEncodeValue($name, $encoding);
86 $email = sprintf('%s %s', $name, $email);
92 public function setEncoding($encoding)
94 $this->encoding
= $encoding;
98 public function getEncoding()
100 if (! $this->encoding
) {
101 $this->encoding
= Mime
::isPrintable($this->getFieldValue(HeaderInterface
::FORMAT_RAW
))
106 return $this->encoding
;
109 public function toString()
111 return 'Sender: ' . $this->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
115 * Set the address used in this header
117 * @param string|\Zend\Mail\Address\AddressInterface $emailOrAddress
118 * @param null|string $name
119 * @throws Exception\InvalidArgumentException
122 public function setAddress($emailOrAddress, $name = null)
124 if (is_string($emailOrAddress)) {
125 $emailOrAddress = new Mail\
Address($emailOrAddress, $name);
126 } elseif (!$emailOrAddress instanceof Mail\Address\AddressInterface
) {
127 throw new Exception\
InvalidArgumentException(sprintf(
128 '%s expects a string or AddressInterface object; received "%s"',
130 (is_object($emailOrAddress) ?
get_class($emailOrAddress) : gettype($emailOrAddress))
133 $this->address
= $emailOrAddress;
138 * Retrieve the internal address from this header
140 * @return \Zend\Mail\Address\AddressInterface|null
142 public function getAddress()
144 return $this->address
;