|
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 $this->_sender = $sender; |
|
34 } |
|
35 |
|
36 /** |
|
37 * Invoked immediately before the Message is sent. |
|
38 * |
|
39 * @param Swift_Events_SendEvent $evt |
|
40 */ |
|
41 public function beforeSendPerformed(Swift_Events_SendEvent $evt) { |
|
42 $message = $evt->getMessage(); |
|
43 $headers = $message->getHeaders(); |
|
44 |
|
45 // save current recipients |
|
46 $headers->addPathHeader('X-Swift-Return-Path', $message->getReturnPath()); |
|
47 |
|
48 // replace them with the one to send to |
|
49 $message->setReturnPath($this->_sender); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Invoked immediately after the Message is sent. |
|
54 * |
|
55 * @param Swift_Events_SendEvent $evt |
|
56 */ |
|
57 public function sendPerformed(Swift_Events_SendEvent $evt) { |
|
58 $message = $evt->getMessage(); |
|
59 |
|
60 // restore original headers |
|
61 $headers = $message->getHeaders(); |
|
62 |
|
63 if ($headers->has('X-Swift-Return-Path')) { |
|
64 $message->setReturnPath($headers->get('X-Swift-Return-Path')->getAddress()); |
|
65 $headers->removeAll('X-Swift-Return-Path'); |
|
66 } |
|
67 } |
|
68 } |