Swiftmailer - Uncaught Error: Call to undefined method Swift_SmtpTransport::newInstance()

Viewed 14581

I'm trying to send email using the Swiftmailer.

I'm getting an Uncaught Error:

Call to undefined method Swift_SmtpTransport::newInstance().

Here is the code:

require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
      ->setUsername ('email@gmail.com')
      ->setPassword ('password');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Weekly Hours')
       ->setFrom (array('email@gmail.com' => 'My Name'))
       ->setTo (array('email@hotmail.com' => 'Recipient'))
       ->setSubject ('Weekly Hours')
       ->setBody ('Test Message', 'text/html');

$result = $mailer->send($message);

Based on the above code, what would be causing that mistake?

3 Answers
Swift_Mailer::newInstance($transport);

The newInstance method is available in version 5.4, in newer version, it is removed. Check version in composer.json.

"swiftmailer/swiftmailer": "^5.4"

add this lines in file : ./swift/classes/Swift/SmtpTransport.php

/**
 * Create a new SmtpTransport instance.
 *
 * @param string  $host
 * @param integer $port
 * @param string  $security
 *
 * @return Swift_SmtpTransport
 */
public static function newInstance($host = 'localhost', $port = 25, $security = null)
{
    return new self($host, $port, $security);
}

it was deleted in new version, but if you update from github, you need everytime add it

Related