Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 protected]')
      ->setPassword ('password');

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

$message = Swift_Message::newInstance('Weekly Hours')
       ->setFrom (array('[email protected]' => 'My Name'))
       ->setTo (array('[email protected]' => 'Recipient'))
       ->setSubject ('Weekly Hours')
       ->setBody ('Test Message', 'text/html');

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

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

like image 777
K Davis Avatar asked Jul 20 '17 23:07

K Davis


1 Answers

I'm not quite familiar with SwiftMailer, but from the brief overview of the error you provided, and their documentation page, I can suggest you to try using new operator. From the error, it's clear that Swift_SmtpTransport class doesn't have a newInstance method, so when you use it to create a new instance, it throws an error. Maybe try using this instead:

require_once 'swift/lib/swift_required.php';

$transport = new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('[email protected]')->setPassword('password');

$mailer = new Swift_Mailer($transport);

$message = new Swift_Message('Weekly Hours');
$message
   ->setFrom(['[email protected]' => 'My Name'])
   ->setTo(['[email protected]' => 'Recipient'])
   ->setSubject('Weekly Hours')
   ->setBody('Test Message', 'text/html');

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

Edit: PHP Doesn't allow a direct method call after instantiating an object (without parenthesis). Thanks, Art Geigel.

like image 110
Luka Kvavilashvili Avatar answered Oct 19 '22 22:10

Luka Kvavilashvili