Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature-mail-headers-messageid' of https://github.com/d…
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 20, 2012
6 parents 5ed923d + 7bfa121 + 0b38f0b + 1914ec7 + 47650bd + 5aed214 commit 2cfde08
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Header/HeaderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class HeaderLoader extends PluginClassLoader
'content-type' => 'Zend\Mail\Header\ContentType',
'date' => 'Zend\Mail\Header\Date',
'from' => 'Zend\Mail\Header\From',
'message-id' => 'Zend\Mail\Header\MessageId',
'mimeversion' => 'Zend\Mail\Header\MimeVersion',
'mime_version' => 'Zend\Mail\Header\MimeVersion',
'mime-version' => 'Zend\Mail\Header\MimeVersion',
Expand Down
121 changes: 121 additions & 0 deletions src/Header/MessageId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mail
*/

namespace Zend\Mail\Header;

use Zend\Mail\Headers;

/**
* @category Zend
* @package Zend_Mail
* @subpackage Header
*/
class MessageId implements HeaderInterface
{
/**
* @var string
*/
protected $messageId;


public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'message-id') {
throw new Exception\InvalidArgumentException('Invalid header line for Message-ID string');
}

$header = new static();
$header->setId($value);

return $header;
}

public function getFieldName()
{
return 'Message-ID';
}

public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
return $this->messageId;
}

public function setEncoding($encoding)
{
// This header must be always in US-ASCII
return $this;
}

public function getEncoding()
{
return 'ASCII';
}

public function toString()
{
return 'Message-ID: ' . $this->getFieldValue();
}

/**
* Set the message id
*
* @return MessageId
*/
public function setId($id = null)
{
if ($id === null) {
$id = $this->createMessageId();
}

$id = sprintf('<%s>', $id);
$this->messageId = $id;
return $this;
}

/**
* Retrieve the message id
*
* @return string
*/
public function getId()
{
return $this->messageId;
}

/**
* Creates the Message-ID
*
* @return string
*/
public function createMessageId() {

$time = time();

if (isset($_SERVER['REMOTE_ADDR'])) {
$user = $_SERVER['REMOTE_ADDR'];
} else {
$user = getmypid();
}

$rand = mt_rand();

if (isset($_SERVER["SERVER_NAME"])) {
$hostName = $_SERVER["SERVER_NAME"];
} else {
$hostName = php_uname('n');
}

return sha1($time . $user . $rand) . '@' . $hostName;
}

}
40 changes: 40 additions & 0 deletions test/Header/MessageIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mail
*/

namespace ZendTest\Mail\Header;

use Zend\Mail\Header;

/**
* @category Zend
* @package Zend_Mail
* @subpackage UnitTests
* @group Zend_Mail
*/
class MessageIdTest extends \PHPUnit_Framework_TestCase
{
public function testSettingManually()
{
$id = "CALTvGe4_oYgf9WsYgauv7qXh2-6=KbPLExmJNG7fCs9B=1nOYg@mail.example.com";
$messageid = new Header\MessageId();
$messageid->setId($id);

$expected = sprintf('<%s>', $id);
$this->assertEquals($expected, $messageid->getFieldValue());
}

public function testAutoGeneration()
{
$messageid = new Header\MessageId();
$messageid->setId();

$this->assertContains('@', $messageid->getFieldValue());
}
}
1 change: 1 addition & 0 deletions test/_files/mail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Subject: multipart
Date: Sun, 01 Jan 2000 00:00:00 +0000
From: =?UTF-8?Q?"Peter M=C3=BCller"?= <[email protected]>
ContENT-type: multipart/alternative; boUNDary="crazy-multipart"
Message-ID: <CALTvGe4_oYgf9WsYgauv7qXh2-6=KbPLExmJNG7fCs9B=1nOYg@mail.example.com>
MIME-version: 1.0

multipart message
Expand Down

0 comments on commit 2cfde08

Please sign in to comment.