ImpersonatePlugin.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2009 Fabien Potencier
  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. * Replaces the sender of a message.
  11. *
  12. * @package Swift
  13. * @subpackage Plugins
  14. * @author Arjen Brouwer
  15. */
  16. class Swift_Plugins_ImpersonatePlugin implements Swift_Events_SendListener
  17. {
  18. /**
  19. * The sender to impersonate.
  20. *
  21. * @var String
  22. * @access private
  23. */
  24. private $_sender;
  25. /**
  26. * Create a new ImpersonatePlugin to impersonate $sender.
  27. *
  28. * @param string $sender address
  29. */
  30. public function __construct($sender)
  31. {
  32. $this->_sender = $sender;
  33. }
  34. /**
  35. * Invoked immediately before the Message is sent.
  36. *
  37. * @param Swift_Events_SendEvent $evt
  38. */
  39. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  40. {
  41. $message = $evt->getMessage();
  42. $headers = $message->getHeaders();
  43. // save current recipients
  44. $headers->addPathHeader('X-Swift-Return-Path', $message->getReturnPath());
  45. // replace them with the one to send to
  46. $message->setReturnPath($this->_sender);
  47. }
  48. /**
  49. * Invoked immediately after the Message is sent.
  50. *
  51. * @param Swift_Events_SendEvent $evt
  52. */
  53. public function sendPerformed(Swift_Events_SendEvent $evt)
  54. {
  55. $message = $evt->getMessage();
  56. // restore original headers
  57. $headers = $message->getHeaders();
  58. if ($headers->has('X-Swift-Return-Path')) {
  59. $message->setReturnPath($headers->get('X-Swift-Return-Path')->getAddress());
  60. $headers->removeAll('X-Swift-Return-Path');
  61. }
  62. }
  63. }