Create an email simulator for your localhost in Magento

Magento sends emails for lots of events, like when you place an order, when a new invoice is generated when a buyer forgets the password. All of these emails need to be checked for proper content as well as design and this could only be done through actual emails getting sent. Sometimes we also need to check some payment methods or sales related emails. So combine all these and it’s easy to say that a local email server is quite the necessity for all Magento developers.

I am going to show a technique here that I learnt while I myself was looking for some mechanism to check my emails are working properly or not. You cannot send mails from a localhost but you can do something that comes pretty close to the results that we want to get.

Magento uses the file called Template.php located here

app\code\core\Mage\Core\Model\Email\

copy this file in your local code pool so that we don’t make any changes in the core file. Around line 453 you’ll find a code like this

try{
    $mail->send();
    $this->_mail = null;
}
    catch (Exception $e) {
    $this->_mail = null;
    Mage::logException($e);
    return false;
}

Replace that code with the following code:

/* START - Email Simulator */
$time = date('dmY_His');
/* END - Email Simulator */
try {
    $mail->send();
    /* START Email Simulator */
    if($this->isPlain()) {
        Mage::log($text, null, 'indies_text_ok_email'.$time.'.log', true);
    } else {
        Mage::log($text, null, 'indies__html_ok_email'.$time.'.html', true);
    }
    /* END Email Simulator */
    $this->_mail = null;
}
    catch (Exception $e) {
    $this->_mail = null;
    /* START Email Simulator */
    if($this->isPlain()) {
        Mage::log($text, null, 'indies_text_error_email'.$time.'.log', true);
    } else {
        Mage::log($text, null, 'indies__html_error_email'.$time.'.html', true);
    }
    /* END Email Simulator */
    Mage::logException($e);
    return false;
}

This little piece of code will write the output of the mail template in a log file that we use to generate system logs or errors. Instead of that we will display our email contents in a new log file created for that. It also checks for email type. If the email template type is text then a text file is created and if it is HTML then an HTML file will be generated in which case you can see in a web browser how your email looks and what can you expect the receiver of that email to see when it is sent in real.

You can check the email log file from

Magento_root/var/log/

An error log will also be generated as the email is not getting sent but don’t worry about that. You’ll also find a new log file with your email content.

There are probably far more elegant solutions than this, you could set up a local email sever using Google apps or other services but this is the simplest of all and starts working in about 5 to 10 minutes without much ado. Here’s hoping it will help someone else too.

Happy coding 🙂

Advertisements

Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: