0

In cakephp documentation it is stated that you can use both text and html template files if you set up like this :

$email = new Email();
$email
    ->template('welcome', 'fancy')
    ->emailFormat('both')
    ->to('bob@example.com')
    ->from('app@domain.com')
    ->send();

This would use the following template files:

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

How does it use both html and text files here? Does it check both text files and html files when creating a view? What if there are differences in those templates?

Jasurbek
  • 68
  • 9

1 Answers1

1

When setting emailFormat to both, CakePHP will put both templates in email, and set Content-type header of message to multipart/alternative. With this behavior, receiving end will be provided with two versions of the same message and will be able to choose which to use.

That means you can provide rich and flashy HTML version of your email to users that can display it, and also provide simple plaintext version of your message which will be used by email clients that can not (or refuse to) render HTML emails.

For more info, please look at this question and answer, as well as at RFC.

Community
  • 1
  • 1
Szymon
  • 1,385
  • 1
  • 8
  • 10
  • Szymon Thank You. I was working on existing project and could not not understand the reason for using both templates. – Jasurbek Dec 04 '18 at 03:17