joi, 25 noiembrie 2010

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  .


To post code in comments, place your code inside [code] and [/code] tags.


View the original article here

Niciun comentariu:

Trimiteți un comentariu