Magento L.E.S.S.

Magento L.E.S.S.

Sometimes its all about title, right?! So, what is LESS? Well, its my fancy acronym for something utterly simply but very practical. It stands for (L)ocalhost (E)mail (S)erver (S)imulator. Basically its neither email server or simulator in the real sense of word. Its just a little trick you can apply on the “app/code/core/Mage/Core/Model/Email/Template.php” file to compensate for lack of the local email server in your development environment while developing for Magento.

Magento hadles lot of email sending, for various cases such as: Register new customer, New order created, Forgot password, etc. One of the downside of not having email server set in your local environment is that you cannot easily test all the transactional emails Magento handles. So if you are assigned task of lets say customizing email templates, possibly adding new variables to email templates, etc., then you need a good testing ground.

Basic idea behind what i call LESS is “find the main/root method for sending emails then add a few lines of logging mechanism to log what is suppose to be send in email”. This way you can easily have a .html or .txt file created with the exact email content that is suppose to go to customer or store owner.

With a little bit of code tracing, you can easily see that the email sending function is located in the previously mentioned “app/code/core/Mage/Core/Model/Email/Template.php” file. Now the easiest, and a bit dirty, way to override this file is to simply copy it into the “app/code/local/Mage/Core/Model/Email/Template.php”. Surely we could create our own module that would implement the changes I will now mentioned, but I don’t think there is a real need, as this is something you can do easily within 5 minutes and you only need to have this locally on your dev machine.

Inside the “app/code/local/Mage/Core/Model/Email/Template.php” file, locate the below shown lines of code, around line 374.

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

And convert them to something like shown below.

/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
$time = date('dmY_His');
/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
 
 
try {
	$mail->send();
 
	/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
	if($this->isPlain()) {
		Mage::log($text, null, 'inchoo_less_email_ok_text_'.$time.'.log', true);
	} else {
		Mage::log($text, null, 'inchoo_less_email_ok_text_'.$time.'.html', true);
	}            
	/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
 
	$this->_mail = null;
}
catch (Exception $e) {
	$this->_mail = null;
 
	/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
	if($this->isPlain()) {
		Mage::log($text, null, 'inchoo_less_email_exception_text_'.$time.'.log', true);
	} else {
		Mage::log($text, null, 'inchoo_less_email_exception_text_'.$time.'.html', true);
	}            
	/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */            
 
	Mage::logException($e);
	return false;
}

Important changes/additions are outlined/surrounded by the START/END LESS comments. As you can see, I am simply adding a Mag::log() call for both successful and failed email sending, just setting the full log name accordingly, with exact time sufix and the appropriate extension suffix. File extension suffix simply depends on the condition of email template being set, either text of HTML format. In case of HTML format I am generating HTML file in he /var/log/folder upon which you can simply double click and it should open in your web browser, where you can see how exactly email should look like once it gets into the customers or site owners mailbox.

Please note that if in our current environment we do not have real email server, code abbove will always generate file with “exception” in the file name. Don’t let this worry you, cause generated log file still holds the valid template content.

Hope this approach helps some of you testing emails in Magento without email server.

I have no doubt there are other quick solutions that one can apply, but this one works for me :).

You made it all the way down here so you must have enjoyed this post! You may also like:

Enabling Multi-part MIME Emails in Magento Tomislav Nikcevski
Tomislav Nikcevski, | 3

Enabling Multi-part MIME Emails in Magento

Extending “Contact us” functionality in Magento Ivan Bernatovic
Ivan Bernatovic, | 8

Extending “Contact us” functionality in Magento

Magento’s transactional eMails do-s and don’t-s Adrian Bece
Adrian Bece, | 10

Magento’s transactional eMails do-s and don’t-s

12 comments

  1. Hvala Branko, after 3 days of eyes burning by my monitor i finally succeed. The problem was in quotes when updating layout with:

    instead of:

    Thank you, now i can sleep again.

  2. Hi, Branko

    Thanks for sharing.

    I use Gmail/Google apps accounts for local development – easy to set up with smtp pro:
    http://www.magentocommerce.com/magento-connect/ASchroder/extension/1865/aschroder.com-smtp-pro

    When I used to code Java a lot, we had a cool little lightweight mailserver that you could use with unit/integration testing, to assert emails were actually being sent (and their contents).

    That’d be quite a nice use for the LESS setup you have, to check that emails have been ‘sent’ from within test code.

  3. Remember to switch off when going to production – or you can quickly run out of disk space on production server and that causes many hard to find issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.