ImpersonatePlugin.php 1.7KB

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