Cakephp 3.x Email Error: Call to undefined method Cake\Mailer\Email::setFrom()


#1

Hi,

While sending email, i am getting an error. Here is my code:

use Cake\Mailer\Email;

$email = new Email(‘default’);

$email->setFrom([‘test@example.com’ => ‘Test Cake Mail’])
->setTo(‘test2@example.com’)
->setSubject(‘Test Mail’)
->send(‘Test mail from CakePhp, please checkout.’);

$email object is created as i have debugged but its methods are said undefined.

I also need to debug the mail at local system rather than i send mail to recipients.

Please give some suggestions to debug mail and send properly.

Regards.


#2

I got that fixed, i looked for Cakephp 3.x documentation and followed code from there. And that version of code wasn’t available at all to the framework version i am using.

I looked into its library and used methods defined there and it worked.

Now, i couldn’t understand how to add another Mail configuration besides default. Any suggestions over this would be great.

Thanks.


#3
  1. As you see, getter and setter are new in 3.4.x and maybe your cakephp version is 3.3.x :grin:

  2. See https://book.cakephp.org/3.0/en/core-libraries/email.html#configuration and https://book.cakephp.org/3.0/en/core-libraries/email.html#creating-custom-transports