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