swift mailer:如何获取最后一条消息(更换后)

时间:2016-01-28 04:32:01

标签: swiftmailer

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

和问题,我怎么能得到已经取代的身体?

在$ mailer->发送($ message)之前

我已经转储了$ message-> getBody();但是身体并没有取代ex:“test [username]”

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

我的期望:“测试我的名字”;

我怎样才能获得已经被装饰者插件取代的身体?需要帮助..谢谢

$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)

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

1 个答案:

答案 0 :(得分:0)

我解决了自己的问题,

在Swift_Plugins_DecoratorPlugin中有3种方法我觉得它对这种情况很重要,

  1. beforeSendPerformed - >替换所有令牌。
  2. sendPerformed - >调用另一个方法_restoreMessage。
  3. _restoreMessage - >恢复身体。
  4. 如何解决: 在你的新类中,只需在调用_restoremessage之前获取sendperfomed中的值,而不是将值抛给新方法。

    在您的新类中继承Swift_Plugins_DecoratorPlugin

    public function sendPerformed(\Swift_Events_SendEvent $evt){
        $this->getValue = $evt->getMessage()->getBody();
        parent::sendPerformed($evt);
    }
    
    public function getFinalBody(){
        return $this->getValue;
    }
    

    感谢