Skip to content

Commit

Permalink
Optimize/more robust address handling in Mdn object
Browse files Browse the repository at this point in the history
No need to convert from mail object -> text -> mail object again.
Fix duplicate internal call to getMdnReturnAddr()
  • Loading branch information
slusarz committed Nov 18, 2014
1 parent 4cd6547 commit 4f03853
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
18 changes: 11 additions & 7 deletions framework/Mime/lib/Horde/Mime/Mdn.php
Expand Up @@ -86,8 +86,9 @@ public function userConfirmationNeeded()
/* RFC 3798 [2.1]: Explicit confirmation is needed if there is more
* than one distinct address in the Disposition-Notification-To
* header. */
$rfc822 = new Horde_Mail_Rfc822();
$addr_ob = $rfc822->parseAddressList($this->getMdnReturnAddr());
$addr_ob = ($hdr = $this->_headers[self::MDN_HEADER])
? $hdr->getAddressList(true)
: array();

switch (count($addr_ob)) {
case 0:
Expand Down Expand Up @@ -162,7 +163,11 @@ public function generate($action, $sending, $type, $name, $mailer,
'from_addr' => null
), $opts);

$to = $this->getMdnReturnAddr();
if (!($hdr = $this->_headers[self::MDN_HEADER])) {
throw new Horde_Mime_Exception();
}

$to = $hdr->getAddressList(true);
$ua = Horde_Mime_Headers_UserAgent::create();

if ($orig_recip = $this->_headers['Original-Recipient']) {
Expand All @@ -179,7 +184,7 @@ public function generate($action, $sending, $type, $name, $mailer,
if ($opts['from_addr']) {
$msg_headers->addHeader('From', $opts['from_addr']);
}
$msg_headers->addHeader('To', $this->getMdnReturnAddr());
$msg_headers->addHeader('To', $to);
$msg_headers->addHeader('Subject', Horde_Mime_Translation::t("Disposition Notification"));

/* MDNs are a subtype of 'multipart/report'. */
Expand Down Expand Up @@ -258,10 +263,9 @@ public function generate($action, $sending, $type, $name, $mailer,
}

/**
* Add a MDN (read receipt) request headers to the Horde_Mime_Headers::
* object.
* Add a MDN (read receipt) request header.
*
* @param string $to The address the receipt should be mailed to.
* @param mixed $to The address(es) the receipt should be mailed to.
*/
public function addMdnRequestHeaders($to)
{
Expand Down
30 changes: 20 additions & 10 deletions framework/Mime/test/Horde/Mime/MdnTest.php
Expand Up @@ -25,24 +25,34 @@ class Horde_Mime_MdnTest extends PHPUnit_Framework_TestCase
/**
* @dataProvider getMdnReturnAddrProvider
*/
public function testGetMdnReturnAddr($h, $expected)
public function testGetMdnReturnAddr($email)
{
$h = new Horde_Mime_Headers();
$ob = new Horde_Mime_Mdn($h);
$this->assertEquals($expected, $ob->getMdnReturnAddr());

if (!is_null($email)) {
$ob->addMdnRequestHeaders($email);
}

$this->assertEquals(
strval($email),
$ob->getMdnReturnAddr()
);
}

public function getMdnReturnAddrProvider()
{
$out = array();

$h = new Horde_Mime_Headers();
$out[] = array(clone $h, null);
$email = 'foo1@example.com, Test <foo2@example.com>';

$email = 'foo@example.com';
$h->addHeader(Horde_Mime_Mdn::MDN_HEADER, $email);
$out[] = array(clone $h, $email);
$rfc822 = new Horde_Mail_Rfc822();
$mail_ob = $rfc822->parseAddressList($email);

return $out;
return array(
array(null),
array('foo@example.com'),
array($email),
array($mail_ob)
);
}

/**
Expand Down

0 comments on commit 4f03853

Please sign in to comment.