2

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?

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
user1658296
  • 1,398
  • 2
  • 18
  • 46
  • Please post your exact CakePHP version – Inigo Flores Dec 14 '15 at 13:09
  • @InigoFlores Hi, it is version 2.7, I should add I have just recently upgraded to 2.7 from 1.3. – user1658296 Dec 14 '15 at 14:07
  • Just to rule it out, have you checked that `/var/www/html/mysite/app/View/Themed/Xyz/Emails/html/add.ctp` is readable by Apache? – Inigo Flores Dec 14 '15 at 14:44
  • Also, you may need to set the theme in CakeEmail - `$Email->theme('xyz')` as described in [Sending Templated Emails](http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-templated-emails). – Inigo Flores Dec 14 '15 at 14:49
  • @InigoFlores Thanks the theme setting has resolved the issue! I really didn't find that clearly communicated in the documentation. – user1658296 Dec 14 '15 at 15:43
  • Great! I will make the comment into an answer so that it can be approved. – Inigo Flores Dec 14 '15 at 15:58
  • Do one thing make a new ctp lets newsletter.ctp ,put it in this path projectname/app/View/Layouts/Emails/html ,here place your template of mail format and create another ctp file same as previous name in projectname/app/View/Emails/html ,copy the code of default.ctp present in this above path into this. just add this line to your controller $mail->template('newsletter', 'newsletter'); and try is it working or not? – Shaddy Dec 16 '15 at 10:59

1 Answers1

2

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.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36