jsdgdo
jsdgdo

Reputation: 65

How do I test an HTML Email Template on CakePHP?

To confirm a purchase I must send an e-mail to the customer with her shopping cart details. I'd like to test the HTML template to use the appropiate CSS, and of course to check the data that arrives to it. What code should I use instead of setting the Email parameters, to watch how the template will render on the Email?

I'm all new on CakePHP, so your help will be very much appreciated.

Thanks in advance.

~José

Upvotes: 6

Views: 10966

Answers (5)

Sandeep Sherpur
Sandeep Sherpur

Reputation: 2802

If you want to print your email template on localhost for testing then you can use this code.

$this->Email->send();
print_r($this->Email->htmlMessage);

Upvotes: 0

Jhanvi
Jhanvi

Reputation: 592

    $this->Email->to = $user_id_array[0]['User']['email'];
    $this->Email->subject = $arrTemplate[0]['EmailTemplate']['subject'];
    $this->Email->replyTo = 'Admin<[email protected]>';
    $this->Email->from = 'Admin<[email protected]>';
    $this->Email->sendAs = 'html';
    $this->Email->template = '/elements/email/html/warning_message';

   $this->set('email_message',$arrTemplate[0]['EmailTemplate']['body']);
   $this->Email->send();

in this way you can set template for email

Upvotes: 1

Faiyaz Alam
Faiyaz Alam

Reputation: 1217

Here is the simple method I use to show html/plain text content of email in browser that I want to send using cakephp email class i.e. App::uses('CakeEmail', 'Network/Email');. Just do an exit at the end of email template file or (email layout file) and then try to send email. it will render the email content.

HTH.

Upvotes: 1

Eric
Eric

Reputation: 827

This is CakePHP 1.3, but I have a feeling it may work well with 2.0 as well. While there may be other ways to do it, I do by creating a test action in any controller, then return a render call of the email template. Check this out:

function email_test()
{
    $this->layout = 'email/html/default';
    $user = $this->User->findById(1);
    $this->set('name', $user['User']['firstname']);
    $this->set('email_heading', 'Welcome to My App');
    return $this->render('/elements/email/html/welcome');
}

This action will now render out your email in the browser.

Upvotes: 11

floriank
floriank

Reputation: 25698

Use the debug transport for testing.

If you want to make it more comfortable write your own transport that creates a new html file in for example APP/tmp/email/.html See the debug transport class as a reference, it's dead easy to do this http://api20.cakephp.org/view_source/debug-transport#l-34

See also the book -> http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#using-transports

Upvotes: 1

Related Questions