Permalink
Browse files

Merge pull request #2860 from anlutro-pull-requests/mailer-failed

Ability to retrieve failed message recipients
  • Loading branch information...
taylorotwell committed Dec 7, 2013
2 parents d3bf82d + 479e512 commit a0086a5779c21a06def56d9cff9bb51a3ed3f242
Showing with 48 additions and 7 deletions.
  1. +18 −1 src/Illuminate/Mail/Mailer.php
  2. +30 −6 tests/Mail/MailMailerTest.php
@@ -53,6 +53,13 @@ class Mailer {
*/
protected $pretending = false;
/**
* Array of failed recipients.
*
* @var array
*/
protected $failedRecipients = array();
/**
* Create a new Mailer instance.
*
@@ -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))
{
@@ -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.
*
@@ -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']);
}
@@ -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']);
}
@@ -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']);
}
@@ -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');
}
@@ -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'));
@@ -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.