Attempted to call an undefined method named “newInstance” of class “Swift_Message”

南笙酒味 提交于 2019-12-04 17:38:44

问题


Since a few days I can't send email anymore using Symfony and Swiftmailer, though I'm using the code from the documentation

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);
}   

and it used to work

and now I can see in the logs

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

what could have changed ?


回答1:


Actually while posting the question and linking to the documentation, I was surprised to see it was updated

Now it is

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

instead of

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

since the release of swiftmailer6 according to the changelog

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

it's a pity there wasn't a "deprecated" period.



来源:https://stackoverflow.com/questions/45447972/attempted-to-call-an-undefined-method-named-newinstance-of-class-swift-messag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!