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
;
10 class MessageId
implements HeaderInterface
17 public static function fromString($headerLine)
19 list($name, $value) = GenericHeader
::splitHeaderLine($headerLine);
20 $value = HeaderWrap
::mimeDecodeValue($value);
22 // check to ensure proper header type for this factory
23 if (strtolower($name) !== 'message-id') {
24 throw new Exception\
InvalidArgumentException('Invalid header line for Message-ID string');
27 $header = new static();
28 $header->setId($value);
33 public function getFieldName()
38 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
40 return $this->messageId
;
43 public function setEncoding($encoding)
45 // This header must be always in US-ASCII
49 public function getEncoding()
54 public function toString()
56 return 'Message-ID: ' . $this->getFieldValue();
62 * @param string|null $id
65 public function setId($id = null)
68 $id = $this->createMessageId();
70 $id = trim($id, '<>');
73 if (! HeaderValue
::isValid($id)
74 ||
preg_match("/[\r\n]/", $id)
76 throw new Exception\
InvalidArgumentException('Invalid ID detected');
79 $this->messageId
= sprintf('<%s>', $id);
84 * Retrieve the message id
88 public function getId()
90 return $this->messageId
;
94 * Creates the Message-ID
98 public function createMessageId()
102 if (isset($_SERVER['REMOTE_ADDR'])) {
103 $user = $_SERVER['REMOTE_ADDR'];
110 if (isset($_SERVER["SERVER_NAME"])) {
111 $hostName = $_SERVER["SERVER_NAME"];
113 $hostName = php_uname('n');
116 return sha1($time . $user . $rand) . '@' . $hostName;