swift 邮件程序:如何获取最后一条消息(替换后)

swift mailer : how to get last message (after replacement)

我们假设所有变量都不为空,并且它工作正常。它发送到我的电子邮件。并且一切正常。

问题是,我怎样才能得到已经替换的 body?

在 $mailer->send($message) 之前我已经转储了 $message->getBody();但是 body 还没有替换 ex:"test [username]"

在 $mailer->send($message) 之后,我得到相同的结果。

我的期望:"test myname";

如何获得已经被装饰器插件替换的 body?需要帮助.. 谢谢

$data = array('[username]'=>$content['username']);
$plugin = new Swift_Plugins_DecoratorPlugin($data);
$message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($this->FROM_ADDRESS, $this->FROM_NAME)
            ->setTo($to)
            ->setBody("test [username]");

$mailer = \Swift_Mailer::newInstance($transport);
        if (!is_null($plugin)) {
            $mailer->registerPlugin($plugin);
        }



$mailer->send($message)

注意:在我的电子邮件中它已经被替换了,没有错。我需要最后一个 body 作为我的日志。所以,消息发送后,它存储到 mylog

我自己解决问题,

在 Swift_Plugins_DecoratorPlugin 中有 3 种方法我认为对这种情况很重要,

  1. beforeSendPerformed -> 替换所有令牌。
  2. sendPerformed -> 调用另一个方法 _restoreMessage。
  3. _restoreMessage -> 恢复正文。

如何解决: 在你的新 class 中,只需在 sendperfomed 调用 _restoremessage 之前获取值,而不是将值扔给你的新方法。

在继承 Swift_Plugins_DecoratorPlugin

的新 class 中
public function sendPerformed(\Swift_Events_SendEvent $evt){
    $this->getValue = $evt->getMessage()->getBody();
    parent::sendPerformed($evt);
}

public function getFinalBody(){
    return $this->getValue;
}

谢谢