0

I'm using cakephp 3.1.6 and I have an admin prefix to separate my administration section. Using this approach I've generated this folder structure for templates:

src/Template
├── Admin
│   ├── Element
│   │   └── ...
│   ├── Email
│   │   └── ...
│   ├── Layout
│   │   └── ...
│   └── ...
├── Element
│   └── ...
├── Email
│   └── ...
├── Layout
│   └── ...
└── ...

It works for normal templates, but it isn't working for email templates. Cakephp is trying to find email templates on default location i.e. src/Template/Email

I've tried using viewBuilder to set the path, like this:

$email = new Email('default');
$email->viewBuilder()->layoutPath(APP . "Template" . DS . "Admin")
      ->templatePath(APP . "Template" . DS . "Admin")
      ->build();

$email->template('forgot_password', 'default')
      ->to($user->email, $user->nick_name)
      ->subject('Reset password')
      ->send();

But it still fails.

Is there any way to change the path for email templates?

Choma
  • 708
  • 12
  • 24

1 Answers1

0

I'll answer my own question since nobody else did it.

The code posted in the question does work in fact, but it has a problem: it sets ONE path, so it just can be used with text OR email template, not both.

So, a better approach (and a more "cake 3" one), would be using themes. This way you can separate templates, helpers and cells; for your admins, public pages, etc.

The code would be something like this:

$email = new Email('default');
$email->template("my_template", "my_layout")
      ->theme("AdminDefaultTheme")
      ->emailFormat('both')
      ->to("someuser@localhost.dev", "Some User")
      ->subject('Testing emails')
      ->send();

and the folders structure would be something like this:

├── plugins     // Your admin templates
│   └── AdminDefaultTheme
│       └── Template
│           ├── Email
│           │  ├── html
│           │  │   └── my_template.ctp
│           │  └── text
│           │      └── my_template.ctp
│           └── Layout
│               └── Email
│                   ├── html
|                   |   └── my_layout.ctp
│                   └── text
|                       └── my_layout.ctp
├── src         // Your app code
│   ├── Controller
│   └── ...
└── ...
Choma
  • 708
  • 12
  • 24