ReporterPlugin.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @author Chris Corbyn
  13. */
  14. class Swift_Plugins_ReporterPlugin implements Swift_Events_SendListener
  15. {
  16. /**
  17. * The reporter backend which takes notifications.
  18. *
  19. * @var Swift_Plugins_Reporter
  20. */
  21. private $reporter;
  22. /**
  23. * Create a new ReporterPlugin using $reporter.
  24. */
  25. public function __construct(Swift_Plugins_Reporter $reporter)
  26. {
  27. $this->reporter = $reporter;
  28. }
  29. /**
  30. * Not used.
  31. */
  32. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  33. {
  34. }
  35. /**
  36. * Invoked immediately after the Message is sent.
  37. */
  38. public function sendPerformed(Swift_Events_SendEvent $evt)
  39. {
  40. $message = $evt->getMessage();
  41. $failures = array_flip($evt->getFailedRecipients());
  42. foreach ((array) $message->getTo() as $address => $null) {
  43. $this->reporter->notify($message, $address, (\array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
  44. }
  45. foreach ((array) $message->getCc() as $address => $null) {
  46. $this->reporter->notify($message, $address, (\array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
  47. }
  48. foreach ((array) $message->getBcc() as $address => $null) {
  49. $this->reporter->notify($message, $address, (\array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
  50. }
  51. }
  52. }