Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2009 Fabien Potencier
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Redirects all email to a single recipient.
13
 * @package Swift
14
 * @subpackage Plugins
15
 * @author Fabien Potencier
16
 */
17
class Swift_Plugins_RedirectingPlugin
18
  implements Swift_Events_SendListener
19
{
20
  /**
21
   * The recipient who will receive all messages.
22
   * @var string
23
   * @access private
24
   */
25
  private $_recipient;
26
 
27
  /**
28
   * Create a new RedirectingPlugin.
29
   * @param int $recipient
30
   */
31
  public function __construct($recipient)
32
  {
33
    $this->_recipient = $recipient;
34
  }
35
 
36
  /**
37
   * Set the recipient of all messages.
38
   * @param int $threshold
39
   */
40
  public function setRecipient($recipient)
41
  {
42
    $this->_recipient = $recipient;
43
  }
44
 
45
  /**
46
   * Get the recipient of all messages.
47
   * @return int
48
   */
49
  public function getRecipient()
50
  {
51
    return $this->_recipient;
52
  }
53
 
54
  /**
55
   * Invoked immediately before the Message is sent.
56
   * @param Swift_Events_SendEvent $evt
57
   */
58
  public function beforeSendPerformed(Swift_Events_SendEvent $evt)
59
  {
60
    $message = $evt->getMessage();
61
    $headers = $message->getHeaders();
62
 
63
    // save current recipients
64
    $headers->addMailboxHeader('X-Swift-To', $message->getTo());
65
    $headers->addMailboxHeader('X-Swift-Cc', $message->getCc());
66
    $headers->addMailboxHeader('X-Swift-Bcc', $message->getBcc());
67
 
68
    // replace them with the one to send to
69
    $message->setTo($this->_recipient);
70
    $headers->removeAll('Cc');
71
    $headers->removeAll('Bcc');
72
  }
73
 
74
  /**
75
   * Invoked immediately after the Message is sent.
76
   *
77
   * @param Swift_Events_SendEvent $evt
78
   */
79
  public function sendPerformed(Swift_Events_SendEvent $evt)
80
  {
81
    $this->_restoreMessage($evt->getMessage());
82
  }
83
 
84
  // -- Private methods
85
 
86
  private function _restoreMessage(Swift_Mime_Message $message)
87
  {
88
    // restore original headers
89
    $headers = $message->getHeaders();
90
 
91
    if ($headers->has('X-Swift-To'))
92
    {
93
      $message->setTo($headers->get('X-Swift-To')->getNameAddresses());
94
      $headers->removeAll('X-Swift-To');
95
    }
96
 
97
    if ($headers->has('X-Swift-Cc'))
98
    {
99
      $message->setCc($headers->get('X-Swift-Cc')->getNameAddresses());
100
      $headers->removeAll('X-Swift-Cc');
101
    }
102
 
103
    if ($headers->has('X-Swift-Bcc'))
104
    {
105
      $message->setBcc($headers->get('X-Swift-Bcc')->getNameAddresses());
106
      $headers->removeAll('X-Swift-Bcc');
107
    }
108
  }
109
}