Swift_Plugins_RedirectingPlugin::beforeSendPerformed PHP Method

beforeSendPerformed() public method

Invoked immediately before the Message is sent.
public beforeSendPerformed ( Swift_Events_SendEvent $evt )
$evt Swift_Events_SendEvent
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
        $message = $evt->getMessage();
        $headers = $message->getHeaders();
        // conditionally save current recipients
        if ($headers->has('to')) {
            $headers->addMailboxHeader('X-Swift-To', $message->getTo());
        }
        if ($headers->has('cc')) {
            $headers->addMailboxHeader('X-Swift-Cc', $message->getCc());
        }
        if ($headers->has('bcc')) {
            $headers->addMailboxHeader('X-Swift-Bcc', $message->getBcc());
        }
        // Filter remaining headers against whitelist
        $this->_filterHeaderSet($headers, 'To');
        $this->_filterHeaderSet($headers, 'Cc');
        $this->_filterHeaderSet($headers, 'Bcc');
        // Add each hard coded recipient
        $to = $message->getTo();
        if (null === $to) {
            $to = array();
        }
        foreach ((array) $this->_recipient as $recipient) {
            if (!array_key_exists($recipient, $to)) {
                $message->addTo($recipient);
            }
        }
    }

Usage Example

 public function testArrayOfRecipientsCanBeExplicitlyDefined()
 {
     $message = Swift_Message::newInstance()->setSubject('...')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo(array('*****@*****.**' => 'Fabien', '*****@*****.**' => 'Chris (To)', '*****@*****.**' => 'Lars (To)'))->setCc(array('*****@*****.**' => 'Fabien', '*****@*****.**' => 'Chris (Cc)', '*****@*****.**' => 'Lars (Cc)'))->setBcc(array('*****@*****.**' => 'Fabien', '*****@*****.**' => 'Chris (Bcc)', '*****@*****.**' => 'John (Bcc)'))->setBody('...');
     $recipients = array('*****@*****.**', '*****@*****.**');
     $patterns = array('/^.*@internal.[a-z]+$/');
     $plugin = new Swift_Plugins_RedirectingPlugin($recipients, $patterns);
     $evt = $this->_createSendEvent($message);
     $plugin->beforeSendPerformed($evt);
     $this->assertEquals($message->getTo(), array('*****@*****.**' => 'Fabien', '*****@*****.**' => 'Lars (To)', '*****@*****.**' => null));
     $this->assertEquals($message->getCc(), array('*****@*****.**' => 'Fabien', '*****@*****.**' => 'Lars (Cc)'));
     $this->assertEquals($message->getBcc(), array('*****@*****.**' => 'Fabien'));
 }
All Usage Examples Of Swift_Plugins_RedirectingPlugin::beforeSendPerformed