1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @see https://github.com/zendframework/zend-mail for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Mail\Header;
use Zend\Mail;
use Zend\Mime\Mime;
/**
* Sender header class methods.
*
* @see https://tools.ietf.org/html/rfc2822 RFC 2822
* @see https://tools.ietf.org/html/rfc2047 RFC 2047
*/
class Sender implements HeaderInterface
{
/**
* @var \Zend\Mail\Address\AddressInterface
*/
protected $address;
/**
* Header encoding
*
* @var null|string
*/
protected $encoding;
public static function fromString($headerLine)
{
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
$value = HeaderWrap::mimeDecodeValue($value);
// check to ensure proper header type for this factory
if (strtolower($name) !== 'sender') {
throw new Exception\InvalidArgumentException('Invalid header name for Sender string');
}
$header = new static();
/**
* matches the header value so that the email must be enclosed by < > when a name is present
* 'name' and 'email' capture groups correspond respectively to 'display-name' and 'addr-spec' in the ABNF
* @see https://tools.ietf.org/html/rfc5322#section-3.4
*/
$hasMatches = preg_match(
'/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
$value,
$matches
);
if ($hasMatches !== 1) {
throw new Exception\InvalidArgumentException('Invalid header value for Sender string');
}
$senderName = trim($matches['name']);
if (empty($senderName)) {
$senderName = null;
}
$header->setAddress($matches['email'], $senderName);
return $header;
}
public function getFieldName()
{
return 'Sender';
}
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
if (! $this->address instanceof Mail\Address\AddressInterface) {
return '';
}
$email = sprintf('<%s>', $this->address->getEmail());
$name = $this->address->getName();
if (! empty($name)) {
if ($format == HeaderInterface::FORMAT_ENCODED) {
$encoding = $this->getEncoding();
if ('ASCII' !== $encoding) {
$name = HeaderWrap::mimeEncodeValue($name, $encoding);
}
}
$email = sprintf('%s %s', $name, $email);
}
return $email;
}
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}
public function getEncoding()
{
if (! $this->encoding) {
$this->encoding = Mime::isPrintable($this->getFieldValue(HeaderInterface::FORMAT_RAW))
? 'ASCII'
: 'UTF-8';
}
return $this->encoding;
}
public function toString()
{
return 'Sender: ' . $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);
}
/**
* Set the address used in this header
*
* @param string|\Zend\Mail\Address\AddressInterface $emailOrAddress
* @param null|string $name
* @throws Exception\InvalidArgumentException
* @return Sender
*/
public function setAddress($emailOrAddress, $name = null)
{
if (is_string($emailOrAddress)) {
$emailOrAddress = new Mail\Address($emailOrAddress, $name);
} elseif (! $emailOrAddress instanceof Mail\Address\AddressInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a string or AddressInterface object; received "%s"',
__METHOD__,
(is_object($emailOrAddress) ? get_class($emailOrAddress) : gettype($emailOrAddress))
));
}
$this->address = $emailOrAddress;
return $this;
}
/**
* Retrieve the internal address from this header
*
* @return \Zend\Mail\Address\AddressInterface|null
*/
public function getAddress()
{
return $this->address;
}
}