Need help with Swift mail program with Kohana shell

My current code is

$swift = email::connect();


        $swift->setSubject('hello')
              ->setFrom(array('alex@example.com.au' => 'Alex'))
              ->setTo(array('alex@example.com.au' => 'Alex'))
              ->setBody('hello')  
              ->attach(Swift_Attachment::fromPath(DOCROOT . 'assets/attachments/instructions.pdf'));

        $swift->send();

email::connect() returns an instance of SwiftMailer .

According to these documents , it looks like it should work.

However, I get an error

Fatal error: Call to undefined method Swift_Mailer::setSubject() in /home/user/public_html/application/classes/controller/properties.php  on line 45

I saw that it email::connect()does exactly what the sample code does in the documents. it

  • include the correct file
  • returns a library instance

What am I doing wrong?

thank

+3
source share
1 answer

You are using an instance Swift_Mailer, and not Swift_Message, as in the example to which you are attached.

I think you need something like this:

$swift = email::connect();
$message = Swift_Message::newInstance();

        $message->setSubject('hello')
              ->setFrom(array('alex@example.com.au' => 'Alex'))
              ->setTo(array('alex@example.com.au' => 'Alex'))
              ->setBody('hello')  
              ->attach(Swift_Attachment::fromPath(DOCROOT . 'assets/attachments/instructions.pdf'));

        $swift->send($message);
+2
source

Source: https://habr.com/ru/post/1737642/


All Articles