Viewing Magento Order Emails on your Local Environment

When testing transactional email templates on a Magento store set up on your local machine, without emails set up to send, it can be difficult to view customisations made. A handy way of viewing Magento order emails on your local environment involves terminating the script in which the emails try and send out using PHP’s exit() or die() functions.

The easiest way to trigger an order email to send is by re-sending a confirmation email from an existing order within the admin area.

This can be achieved using by pressing the Send Email button.

Viewing Magento Order Emails on your Local Environment

One of the main classes responsible for sending transactional emails in Magento is the Mage_Core_Model_Email_Template class.

Within the send() method, the Zend_Mail instance, stored in the $mail variable, uses the setBodyText method to set the email body content.

// app/code/core/Mage/Core/Model/Email/Template.php

class Mage_Core_Model_Email_Template extends Mage_Core_Model_Email_Template_Abstract
{

    ....

    public function send($email, $name = null, array $variables = array())
    {
        ....

        if ($this->isPlain()) {
            $mail->setBodyText($text);
        } else {
            $mail->setBodyHTML($text);
        }

        ....
    }

    ....

}

The $text variable contains the email content of the order, including specific order variables that cannot be viewed within the System -> Transactional Emails section in the admin.

Simply echo out $text and terminate the execution below these lines.

if ($this->isPlain()) {
    $mail->setBodyText($text);
} else {
    $mail->setBodyHTML($text);
}
echo $text; exit();

You might also need to remove the hasQueue() conditional statement above if your emails are being queued by Magento.

// Comment out this check
if ($this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {
    /** @var $emailQueue Mage_Core_Model_Email_Queue */
    $emailQueue = $this->getQueue();
    $emailQueue->setMessageBody($text);
    $emailQueue->setMessageParameters(array(
        'subject'           => $subject,
        'return_path_email' => $returnPathEmail,
        'is_plain'          => $this->isPlain(),
        'from_email'        => $this->getSenderEmail(),
        'from_name'         => $this->getSenderName(),
        'reply_to'          => $this->getMail()->getReplyTo(),
        'return_to'         => $this->getMail()->getReturnPath(),
    ))
    ->addRecipients($emails, $names, Mage_Core_Model_Email_Queue::EMAIL_TYPE_TO)
    ->addRecipients($this->_bccEmails, array(), Mage_Core_Model_Email_Queue::EMAIL_TYPE_BCC);
    $emailQueue->addMessageToQueue();

    return true;
}

Click Send Email within the admin, and you will see the email template visible in the browser.

Viewing Magento Order Emails on your Local Environment

There may be occasions where third party extensions installed will override this send() method. For example, the Ebizmarts MageMonkey extension does this.

In this situation you will need to locate the class responsible for extending Mage_Core_Model_Email_Template and copy the above modifications into this class.

You can grep or search for a extends Mage_Core_Model_Email_Template string within your project to find any classes that extend Magento’s default functionality.

Note: This article is based on Magento CE version 1.9.