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

2

14

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('[email protected]')
        ->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 ?

Silber answered 1/8, 2017 at 21:29 Comment(0)
S
28

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.

Silber answered 1/8, 2017 at 21:33 Comment(0)
L
1

@allan.simon answer is right, but it might be simpler.

You can, but there is no need to pass $subject into __constructor.

Before

$message = \Swift_Message::newInstance()

Now

$message = (new \Swift_Message())
Lexeme answered 11/6, 2020 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.