phpcakephpcakephp-2.7cakeemail

CakeEmail is erroneously reporting that a template is missing when using themes


I have a controller QuickContacts with an action add() which uses CakeEmail to send a message, like so:

$Email = new CakeEmail();
$Email->from(array('noreply@xyz' => 'xyz'));
$Email->to(($this->isBranded) ? $this->brandedAccount['BrandedAccount']['contact_us_email'] : EMAIL_TO_MAIL_ADDRESS);
$Email->subject(EMAIL_QUICK_CONTACTS_SUBJECT);
$Email->emailFormat('html')->template('add', 'default');
$Email->message($this->request->data);

$Email->send();

When I try and send the mail, I get an error indicating that a view file does not exist:

Missing View
Error: The view for QuickContactsController::add() was not found.

Confirm you have created the file: Emails/html/add.ctp  in one of the following paths:

/var/www/html/mysite/app/View/Themed/Xyz/Emails/html/add.ctp

I can certainly confirm those files exist, but for some reason CakePHP still is unable to find them, and I can't figure out why this might be happening. Can anyone point me in the right direction?


Solution

  • You have to explicitly set the theme in CakeEmail:

    $Email->theme('xyz') 
    

    as described in the Cookboox 2.x: Sending Templated Emails.

    The error message is pointing you towards the correct path, but CakePHP is looking for your file in:

    /app/Emails/html/add.ctp
    

    This may be due to a bug.