ReporterPlugin.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Does real time reporting of pass/fail for each recipient.
  11. *
  12. * @package Swift
  13. * @subpackage Plugins
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Plugins_ReporterPlugin implements Swift_Events_SendListener
  17. {
  18. /**
  19. * The reporter backend which takes notifications.
  20. *
  21. * @var Swift_Plugin_Reporter
  22. */
  23. private $_reporter;
  24. /**
  25. * Create a new ReporterPlugin using $reporter.
  26. *
  27. * @param Swift_Plugins_Reporter $reporter
  28. */
  29. public function __construct(Swift_Plugins_Reporter $reporter)
  30. {
  31. $this->_reporter = $reporter;
  32. }
  33. /**
  34. * Not used.
  35. */
  36. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  37. {
  38. }
  39. /**
  40. * Invoked immediately after the Message is sent.
  41. *
  42. * @param Swift_Events_SendEvent $evt
  43. */
  44. public function sendPerformed(Swift_Events_SendEvent $evt)
  45. {
  46. $message = $evt->getMessage();
  47. $failures = array_flip($evt->getFailedRecipients());
  48. foreach ((array) $message->getTo() as $address => $null) {
  49. $this->_reporter->notify(
  50. $message, $address, (array_key_exists($address, $failures)
  51. ? Swift_Plugins_Reporter::RESULT_FAIL
  52. : Swift_Plugins_Reporter::RESULT_PASS)
  53. );
  54. }
  55. foreach ((array) $message->getCc() as $address => $null) {
  56. $this->_reporter->notify(
  57. $message, $address, (array_key_exists($address, $failures)
  58. ? Swift_Plugins_Reporter::RESULT_FAIL
  59. : Swift_Plugins_Reporter::RESULT_PASS)
  60. );
  61. }
  62. foreach ((array) $message->getBcc() as $address => $null) {
  63. $this->_reporter->notify(
  64. $message, $address, (array_key_exists($address, $failures)
  65. ? Swift_Plugins_Reporter::RESULT_FAIL
  66. : Swift_Plugins_Reporter::RESULT_PASS)
  67. );
  68. }
  69. }
  70. }