Skip to content

Commit

Permalink
Report RFC exceptions as send failures (if sending a message) rather …
Browse files Browse the repository at this point in the history
…than letting the bubble up.
  • Loading branch information
d11wtq authored and fabpot committed May 17, 2011
1 parent 9729315 commit acb505a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/classes/Swift/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
$this->_transport->start();
}

return $this->_transport->send($message, $failedRecipients);
$sent = 0;

try
{
$sent = $this->_transport->send($message, $failedRecipients);
}
catch (Swift_RfcComplianceException $e)
{
foreach ($message->getTo() as $address => $name)
{
$failedRecipients[] = $address;
}
}

return $sent;
}

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Swift/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once 'Swift/Tests/SwiftUnitTestCase.php';
require_once 'Swift/Mailer.php';
require_once 'Swift/RfcComplianceException.php';
require_once 'Swift/Transport.php';
require_once 'Swift/Mime/Message.php';
require_once 'Swift/Mailer/RecipientIterator.php';
Expand Down Expand Up @@ -251,6 +252,48 @@ public function testBatchSendCanReadFromIterator()
$mailer->batchSend($message, $failures, $it);
}

public function testSendRecordsRfcComplianceExceptionAsEntireSendFailure()
{
$failures = array();

$rfcException = new Swift_RfcComplianceException('test');
$transport = $this->_createTransport();
$message = $this->_createMessage();
$this->_checking(Expectations::create()
-> allowing($message)->getTo() -> returns(array('foo&invalid' => 'Foo', '[email protected]' => 'Bar'))
-> one($transport)->send($message, reference($failures)) -> throws($rfcException)
-> ignoring($transport)
-> ignoring($message)
);

$mailer = $this->_createMailer($transport);
$this->assertEqual(0, $mailer->send($message, $failures), '%s: Should return 0');
$this->assertEqual(array('foo&invalid', '[email protected]'), $failures, '%s: Failures should contain all addresses since the entire message failed to compile');
}

public function testBatchSendRecordsRfcComplianceExceptionAsIndividualRecipientFailure()
{
$failures = array();

$rfcException = new Swift_RfcComplianceException('test');
$transport = $this->_createTransport();
$message = $this->_createMessage();
$this->_checking(Expectations::create()
-> one($message)->getTo() -> returns(array('foo&invalid' => 'Foo', '[email protected]' => 'Bar'))
-> one($message)->setTo(array('foo&invalid' => 'Foo'))
-> one($message)->getTo() -> returns(array('foo&invalid' => 'Foo'))
-> one($message)->setTo(array('[email protected]' => 'Bar'))
-> one($transport)->send($message, reference($failures)) -> throws($rfcException)
-> one($transport)->send($message, reference($failures)) -> returns(1)
-> ignoring($transport)
-> ignoring($message)
);

$mailer = $this->_createMailer($transport);
$this->assertEqual(1, $mailer->batchSend($message, $failures), '%s: Should return just 1');
$this->assertEqual(array('foo&invalid'), $failures, '%s: Failures should contain the non-compliant address');
}

public function testRegisterPluginDelegatesToTransport()
{
$plugin = $this->_createPlugin();
Expand Down

0 comments on commit acb505a

Please sign in to comment.