Skip to content

Commit

Permalink
Merge pull request #2860 from anlutro-pull-requests/mailer-failed
Browse files Browse the repository at this point in the history
Ability to retrieve failed message recipients
  • Loading branch information
taylorotwell committed Dec 7, 2013
2 parents d3bf82d + 479e512 commit a0086a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
19 changes: 18 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Expand Up @@ -53,6 +53,13 @@ class Mailer {
*/
protected $pretending = false;

/**
* Array of failed recipients.
*
* @var array
*/
protected $failedRecipients = array();

/**
* Create a new Mailer instance.
*
Expand Down Expand Up @@ -290,7 +297,7 @@ protected function sendSwiftMessage($message)
{
if ( ! $this->pretending)
{
return $this->swift->send($message);
return $this->swift->send($message, $this->failedRecipients);
}
elseif (isset($this->logger))
{
Expand Down Expand Up @@ -399,6 +406,16 @@ public function getSwiftMailer()
return $this->swift;
}

/**
* Get the array of failed recipients.
*
* @return array
*/
public function getFailedRecipients()
{
return $this->failedRecipients;
}

/**
* Set the Swift Mailer instance.
*
Expand Down
36 changes: 30 additions & 6 deletions tests/Mail/MailMailerTest.php
Expand Up @@ -23,7 +23,7 @@ public function testMailerSendSendsMessageWithProperViewContent()
$message->shouldReceive('setFrom')->never();
$mailer->setSwiftMailer(m::mock('StdClass'));
$message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, array());
$mailer->send('foo', array('data'), function($m) { $_SERVER['__mailer.test'] = $m; });
unset($_SERVER['__mailer.test']);
}
Expand All @@ -44,7 +44,7 @@ public function testMailerSendSendsMessageWithProperPlainViewContent()
$message->shouldReceive('setFrom')->never();
$mailer->setSwiftMailer(m::mock('StdClass'));
$message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, array());
$mailer->send(array('foo', 'bar'), array('data'), function($m) { $_SERVER['__mailer.test'] = $m; });
unset($_SERVER['__mailer.test']);
}
Expand All @@ -65,7 +65,7 @@ public function testMailerSendSendsMessageWithProperPlainViewContentWhenExplicit
$message->shouldReceive('setFrom')->never();
$mailer->setSwiftMailer(m::mock('StdClass'));
$message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, array());
$mailer->send(array('html' => 'foo', 'text' => 'bar'), array('data'), function($m) { $_SERVER['__mailer.test'] = $m; });
unset($_SERVER['__mailer.test']);
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public function testMailerCanResolveMailerClasses()
$message->shouldReceive('setFrom')->never();
$mailer->setSwiftMailer(m::mock('StdClass'));
$message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message);
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, array());
$mailer->send('foo', array('data'), 'FooMailer');
}

Expand All @@ -185,14 +185,30 @@ public function testGlobalFromIsRespectedOnAllMessages()
$mailer->setSwiftMailer(m::mock('StdClass'));
$mailer->alwaysFrom('taylorotwell@gmail.com', 'Taylor Otwell');
$me = $this;
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type('Swift_Message'))->andReturnUsing(function($message) use ($me)
$mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type('Swift_Message'), array())->andReturnUsing(function($message) use ($me)
{
$me->assertEquals(array('taylorotwell@gmail.com' => 'Taylor Otwell'), $message->getFrom());
});
$mailer->send('foo', array('data'), function($m) {});
}


public function testFailedRecipientsAreAppendedAndCanBeRetrieved()
{
unset($_SERVER['__mailer.test']);
$mailer = $this->getMailer();
$view = m::mock('StdClass');
$mailer->getViewEnvironment()->shouldReceive('make')->once()->andReturn($view);
$view->shouldReceive('render')->once()->andReturn('rendered.view');
$swift = new FailingSwiftMailerStub;
$mailer->setSwiftMailer($swift);

$mailer->send('foo', array('data'), function($m) {});

$this->assertEquals(array('taylorotwell@gmail.com'), $mailer->getFailedRecipients());
}


protected function getMailer()
{
return new Illuminate\Mail\Mailer(m::mock('Illuminate\View\Environment'), m::mock('Swift_Mailer'));
Expand All @@ -204,4 +220,12 @@ protected function getMocks()
return array(m::mock('Illuminate\View\Environment'), m::mock('Swift_Mailer'));
}

}
}

class FailingSwiftMailerStub
{
public function send($message, &$failed)
{
$failed[] = 'taylorotwell@gmail.com';
}
}

0 comments on commit a0086a5

Please sign in to comment.