ImpersonatePlugin.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. * @author Arjen Brouwer
  13. */
  14. class Swift_Plugins_ImpersonatePlugin implements Swift_Events_SendListener
  15. {
  16. /**
  17. * The sender to impersonate.
  18. *
  19. * @var string
  20. */
  21. private $sender;
  22. /**
  23. * Create a new ImpersonatePlugin to impersonate $sender.
  24. *
  25. * @param string $sender address
  26. */
  27. public function __construct($sender)
  28. {
  29. $this->sender = $sender;
  30. }
  31. /**
  32. * Invoked immediately before the Message is sent.
  33. */
  34. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  35. {
  36. $message = $evt->getMessage();
  37. $headers = $message->getHeaders();
  38. // save current recipients
  39. $headers->addPathHeader('X-Swift-Return-Path', $message->getReturnPath());
  40. // replace them with the one to send to
  41. $message->setReturnPath($this->sender);
  42. }
  43. /**
  44. * Invoked immediately after the Message is sent.
  45. */
  46. public function sendPerformed(Swift_Events_SendEvent $evt)
  47. {
  48. $message = $evt->getMessage();
  49. // restore original headers
  50. $headers = $message->getHeaders();
  51. if ($headers->has('X-Swift-Return-Path')) {
  52. $message->setReturnPath($headers->get('X-Swift-Return-Path')->getAddress());
  53. $headers->removeAll('X-Swift-Return-Path');
  54. }
  55. }
  56. }