【问题标题】:Attempted to call an undefined method named "newInstance" of class "Swift_Message"试图调用类“Swift_Message”的名为“newInstance”的未定义方法
【发布时间】:2017-08-01 21:29:59
【问题描述】:

几天以来,我无法再使用 Symfony 和 Swiftmailer 发送电子邮件,尽管我使用的是文档中的代码

private function _sendResetPasswordEmail(UserInterface $user)
{   
    $subject = $this->get('translator')->trans('email-title-reset-password');
    $message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom('contact@example.com')
        ->setTo($user->getEmail())
        ->setBody(
            $this->renderView(
                'reset-password-email.html.twig',
                ['user' => $user]
            ),
            'text/html'
        )
    ;
    $this->get('mailer')->send($message);
}   

它曾经工作过

现在我可以在日志中看到

"Attempted to call an undefined method named "newInstance" of class "Swift_Message"

会发生什么变化?

【问题讨论】:

    标签: symfony swiftmailer


    【解决方案1】:

    实际上,在发布问题并链接到文档时,我很惊讶地看到它已更新

    现在是

     $message = (new \Swift_Message('Hello Email'))
    

    而不是

     $message = \Swift_Message::newInstance()
           ->setSubject('Hello Email')
    

    根据变更日志,自 swiftmailer6 发布以来

    https://github.com/swiftmailer/swiftmailer/blob/master/CHANGES#L24

    很遗憾没有“弃用”时期。

    【讨论】:

      【解决方案2】:

      @allan.simon 的答案是正确的,但它可能更简单。

      可以,但无需将$subject 传递给__constructor

      之前

      $message = \Swift_Message::newInstance()
      

      现在

      $message = (new \Swift_Message())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-27
        • 2015-12-20
        相关资源
        最近更新 更多