Debugging CakeEmail

I'm trying to set up a test email in my controller that outputs the email the way it would look like without actually sending the email. I am using CakePHP 2.1

At the top of my controller, I have:

App::uses('CakeEmail', 'Network/Email');


And in my controller the method:

$email = new CakeEmail('default');  
$email->from(array('' => 'My Site'));  
$email->send('My message');


In my email config, I set the $ default mode for Debug:

public $default = array(
    'transport' => 'Debug',
    'from' => '',
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',


How can I send a message? I looked through google and came back with nothing.


source to share

3 answers

Build CakeEmail with log included:

$Email = new CakeEmail(array('log' => true));


The following code is a snippet of CakeEmail's send () method and should be pretty self-learning.

    $contents = $this->transportClass()->send($this);
    if (!empty($this->_config['log'])) {
        $level = LOG_DEBUG;
        if ($this->_config['log'] !== true) {
            $level = $this->_config['log'];
        CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message']);
    return $contents;


This will put your email address in this log file.

If you don't like it, feel free to write your own transport class and register with the database, session, or just debug () the output in your transport class, do as you like!



The send () method actually returns an array containing the email headers and body (each as a string). So if you are writing unit tests, you can search these lines to see if they contain what they need.

If you want to run the send command without sending the email, you can change the transport method to a Debug transport, which goes through the email formatting without actually sending it.

For example:

$email = new CakeEmail();
$response = $email->send();
echo $response['headers']; // headers as string
echo $response['message']; // message body with attachments


Note. If you are sending HTML messages and / or using attachments, the body will contain these elements in some kind of encoded form. If you send plain and encoded HTML messages, some content will be rendered twice (once and again as HTML).

Like Drawrdesign, I plan on providing a browser preview of my email. There are two methods:

  • Parse the "message" returned by send () and print the appropriate sections.
  • Just create my email template like a normal Cake template and result, result.

I will try to update my answer as soon as I have implemented the solution.



You can view your email and how it will look without even sending it. This is a neat little trick I discovered and is quite useful, although it can be a little "hacky":

The trick is to map your email, including any data, to a specific layout and template, and then render that structure before sending it using the email component. I came up with the following function to check emails (debug them):

public function debugEmail() {

    // set some data for your email
    $data = 'foo';

    // setup layout and a View instance
    $this->layout = 'Emails/html/default';
    $View = new View($this, false);

    // render the email template including the layout into a variable
    $html = $View->render('../Emails/html/cron/your_template');

    // print the contents on screen (do NOT use pr() here!)


The example uses a layout customization for the default html email layout and a template named your_template. It just maps the email template created within the given layout to a variable and prints it to the screen. The browser will interpret the HTML and you will see how your email will look.

Note. Some email clients will display your HTML in a slightly different way. You will need to use inline styles (CSS) and cross-programmatically check to make sure everything is in place. The function is mainly used to check the correct structuring and display of data in my emails.



All Articles