Index: library/Zend/Mail.php =================================================================== --- library/Zend/Mail.php (revision 10937) +++ library/Zend/Mail.php (working copy) @@ -115,6 +115,12 @@ protected $_date = null; /** + * Message-ID: header + * @var string + */ + protected $_messageId = null; + + /** * text/plain MIME part * @var false|Zend_Mime_Part */ @@ -461,6 +467,28 @@ } /** + * Helper function for creating a RFC 2822 compliant Message-ID header + * + * Inspired by MailMan implementation at + * http://mail.python.org/pipermail/mailman-developers/2002-April/011407.html + * + * Sample Message-ID: + * <20020201195627.33539.96671@host1.mydomain.com> + */ + protected function _createMessageId() + { + $utcDate = gmstrftime('%Y%m%d%H%M%S'); + $pid = getmypid(); + $rand = mt_rand(0,100000); + $hostName = php_uname('n'); + if (isset($_SERVER["SERVER_NAME"])) { + $hostName = $_SERVER["SERVER_NAME"]; + } + $messageId = vsprintf('<%s.%s.%s@%s>', array($utcDate, $pid, $rand, $hostName)); + return $messageId; + } + + /** * Adds To-header and recipient * * @param string $email @@ -660,6 +688,41 @@ } /** + * Sets Message-ID header + * + * @param string $id + * @return Zend_Mail Provides fluent interface + */ + public function setMessageId($id = null) + { + if ($this->_messageId === null ) { + if ($id === null) { + $this->_messageId = $this->_createMessageId(); + } else { + $this->_messageId = $id; + } + $this->_storeHeader('Message-Id', $this->_messageId); + } else { + /** + * @see Zend_Mail_Exception + */ + require_once 'Zend/Mail/Exception.php'; + throw new Zend_Mail_Exception('Message-ID Header set twice'); + } + return $this; + } + + /** + * Returns the Message-ID of the email + * + * @return string + */ + public function getMessageId() + { + return $this->_messageId; + } + + /** * Add a custom header to the message * * @param string $name @@ -718,6 +781,10 @@ $this->setDate(); } + if (is_null($this->_messageId)) { + $this->setMessageId(); + } + $transport->send($this); return $this;