11

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?

azro
  • 53,056
  • 7
  • 34
  • 70
K Davis
  • 121
  • 1
  • 1
  • 5

3 Answers3

19

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@gmail.com')->setPassword('password');

$mailer = new Swift_Mailer($transport);

$message = new Swift_Message('Weekly Hours');
$message
   ->setFrom(['email@gmail.com' => 'My Name'])
   ->setTo(['email@hotmail.com' => '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.

Luka Kvavilashvili
  • 1,309
  • 10
  • 13
  • 1
    I needed to put parentheses around the new object to get it to work like so: $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl')) – Art Geigel Mar 20 '18 at 23:25
  • Yeah, forgot that PHP doesn't allow direct usage of the instance. Personally I don't find parenthesis around the object very readable, so I'll leave it the simple way haha. Thanks for your comment! @ArtGeigel – Luka Kvavilashvili Apr 02 '19 at 16:43
0
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"
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

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

Kamil Dąbrowski
  • 984
  • 11
  • 17