0

I have upgraded to Cakephp 3.4 and now doing my code migrations. I modified my email functions to the following:

 $email->setTemplate('welcome', 'registration')
       ->setSubject($subject)
       ->setEmailFormat('html')  
       ->setTo($toEmail)
       ->setFrom($fromEmail)
       ->send();

But the emails are being sent without any templates. However, if I change it back to

$email->template('welcome', 'registration')

then the emails are again sent with template. The setTemplate() function doesn't seem to be working for me. Can anybody please help me figure out what I am missing.

Edit: The html file is located at src/Template/Email/html/welcome.ctp and the layout is located at src/Template/Layout/Email/html/registration.ctp

EssEss
  • 73
  • 10
  • Is template located correctly ? Where is it ? – Manohar Khadka Feb 27 '17 at 06:17
  • The `setTemplate()` method doesn't take a second argument, layouts are ment to be set via `setLayout()`. – ndm Feb 27 '17 at 13:05
  • Thanks a lot for your responses. @ndm : I tried setting the layout using setLayout() as per your suggestion and it is working. However, the [Sending Templated Emails](https://book.cakephp.org/3.0/en/core-libraries/email.html#sending-templated-emails) section of the documentation describes with the example `$email->setTemplate('welcome', 'fancy')` for using layouts. We have used the above format to set the email layouts through out our application. I would like to confirm this before making the whole changes. Thanks – EssEss Feb 28 '17 at 00:05
  • Those cookbook examples are definitely incorrect. – ndm Feb 28 '17 at 00:25
  • https://github.com/cakephp/docs/pull/4760 – ndm Feb 28 '17 at 01:28
  • Thanks for the quick response! – EssEss Feb 28 '17 at 01:35

2 Answers2

3

As already mentioned in the comments, unlike the deprecated Email::template() method, which can be used to set the template as well as the layout, Email::setTemplate() does not take a second argument, and only sets the template.

With the newly introduced setter methods, layouts are ment to be set via the separate Email::setLayout() method. The examples in the Cookbook for sending templated emails were incorrect, and have now been fixed.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110
-2

/// Email format HTML

$email->setTemplate('welcome', 'registration') ->setEmailFormat('html') ->setTo('bob@example.com') ->setFrom('app@domain.com') ->send();

This would use the following template files:

src/Template/Email/html/welcome.ctp src/Template/Layout/Email/html/registration.ctp

/// Email format TEXT

$email->setTemplate('welcome', 'registration') ->setEmailFormat('text') ->setTo('bob@example.com') ->setFrom('app@domain.com') ->send();

This would use the following template files:

src/Template/Email/text/welcome.ctp src/Template/Layout/Email/text/registration.ctp

/// Email format BOTH

$email->setTemplate('welcome', 'registration') ->setEmailFormat('both') ->setTo('bob@example.com') ->setFrom('app@domain.com') ->send();

This would use the following template files:

src/Template/Email/text/welcome.ctp src/Template/Layout/Email/text/registration.ctp src/Template/Email/html/welcome.ctp src/Template/Layout/Email/html/registration.ctp