37

I'm editing some phtml files which are included in transactional emails in Magento, but in order to test the changes, I have to actually send the emails. In the case of (e.g.) the order confirmation email this means I have to place an order each time I want to test an email!

Choosing "preview email" in the backend does not help because the email contains no visible order data.

Is there a way to preview a transactional email but have it render with order data?

5
  • Could you hardcode it to send the email to you?
    – Wing Lian
    Dec 11, 2012 at 16:13
  • As part of the "Preview Email" function, you mean? Possibly, though I'm not sure how. And I don't know how I'd inject order data into it before sending either.
    – WackGet
    Dec 11, 2012 at 16:26
  • 12
    It's always a good idea to create a dev environment for situation like this. Also you dont have to create a new order, you could resend a previous order email (by going to that order and click resend email) Dec 11, 2012 at 16:59
  • Resend email is a great tip. Thank you. If you'd like to put this as the official answer I will accept it. Otherwise I'll answer it.
    – WackGet
    Dec 12, 2012 at 9:54
  • What about all the other email notifications?
    – ermannob
    Apr 30, 2014 at 7:14

11 Answers 11

38

From user R.S:

You dont have to create a new order, you could resend a previous order email (by going to that order and click resend email).

That's the closest thing I've found to quickly re-test emails when playing around with the templates. Thanks R.S!

1
  • 1
    If new order email has already been sent, it may not be sent again. Check if Mage_Sales_Model_Order::sendNewOrderEmail() has a if ($this->getEmailSent()) { return $this; } test. If so, temporarily comment this out. Apr 3, 2014 at 23:27
19

The following snippet will render the "new sales order" email and displays it for any given order. Put the following in /test.php for example and just browse to the proper location like http://www.example.com/test.php

require_once 'app/Mage.php';
Mage::app();

// loads the proper email template
$emailTemplate  = Mage::getModel('core/email_template')
                      ->loadDefault('sales_email_order_template');

// All variables your error log tells you that are missing can be placed like this:

$emailTemplateVars = array();
$emailTemplateVars['usermessage'] = "blub";
$emailTemplateVars['store'] = Mage::app()->getStore();
$emailTemplateVars['sendername'] = 'sender name';
$emailTemplateVars['receivername'] = 'receiver name';

// order you want to load by ID

$emailTemplateVars['order'] = Mage::getModel('sales/order')->load(673);

// load payment details:
// usually rendered by this template:
// web/app/design/frontend/base/default/template/payment/info/default.phtml
$order = $emailTemplateVars['order'];
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
                ->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore(Mage::app()->getStore()); 

$emailTemplateVars['payment_html'] = $paymentBlock->toHtml();

//displays the rendered email template
echo $emailTemplate->getProcessedTemplate($emailTemplateVars);
2
  • 1
    Please add more information to your code so that following visitors of this site can benefit from your answer!
    – KhorneHoly
    Feb 19, 2015 at 7:57
  • It actually works great! Thank you so much! This saves so much emails :D May 12, 2020 at 8:05
15

For sales orders, I use a test account and a script I have in my root directory.

the script looks like this:

<?php
include 'app/Mage.php';
Mage::app('default');

$_order = Mage::getModel('sales/order')->load($argv[1]);
$_order->sendNewOrderEmail(); 

and I call it like:

php -f sendTestEmail.php -- 4303 

where 4303 is the order I have used for testing before.

hope that helps.

2
  • 2
    This is an ideal use case for a web inaccessible shell script. If a script-kiddie were to find this in the webroot, holy spam batman. Sep 25, 2016 at 3:18
  • 1
    @LukeA.Leber if it where only on dev no worries, just make sure it doesnt end up in a public access map
    – Bram B
    Jul 25, 2018 at 15:18
4

We developed a free open-source extension called Hackathon_EmailPreview.

Concerning order mails you can preview them in the browser without sending and you can enter any order number to check what different orders would look like.

4
  • 1
    There's no help to "how to use" your extension. You should add it to your doc because there's only an installation note.
    – RPDeshaies
    Jul 24, 2014 at 13:25
  • 1
    That's true. Thanks for the input. Jul 24, 2014 at 18:02
  • Hi, I've tested your module on Magento 1.9 and it's not working. After clicking on Preview button, we get an error, something like "call payment instance", I don't remember exactly, but it's not working on 1.9 version. Apr 29, 2017 at 2:48
  • That might be true. This was a Hackathon project and is not an actively maintained project. As stated in the readme, it was only tested with CE 1.7.0.2 (which was the current version as of the Hackathon). Apr 29, 2017 at 22:13
4

Follow the instructions in this blogpost: Magento (L)ocalhost (E)mail (S)erver (S)imulator (Nov 2010; by Branko Ajzele) Magento will save the html e-mails, including all the variables you're trying to send in var/log.

2
  • This is great too, and will surely come in useful. Thank you. The only thing it doesn't do is get rid of the need for me to keep placing orders. However using R.S's tip, above, about "resend email" this will be easy.
    – WackGet
    Dec 12, 2012 at 16:21
  • That's true. Keep resending the same order to finetune your transactional email. Dec 13, 2012 at 3:58
3

We have actually developed an extension that allows you to do exactly this: Test send any Magento transactional email from within the Magento backend. Hope it's not too commercial to mention it here.: http://www.yireo.com/software/magento-extensions/emailtester

3
  • 4
    StackOverflow isn't a place to post a link to a paid extension, especially when your answer is just the link with no helpful words.
    – swl1020
    Dec 23, 2013 at 23:37
  • 2
    and this buddy is just proposing a solution, i dont think any SO rule is violated here... Sep 16, 2014 at 23:16
  • Well the answer is promotional. But all I can do is leave a "promotional" comment as well, as I'm using that extension at work and it does the work and is a nice-to-have.
    – hakre
    Jul 26, 2015 at 20:17
1

You can enter backend, go to customers, select your own customer user, and at the bottom you will have "Send Auto-Generated Password". Insert admin password and click "Save and continue editing". This way you will send a password to your own account and test your template.

1

I have released an extension allowing you to preview transactional mails from your back-office. You just have to pass one order, and then a mail preview button will be available on it in the BO. Additionally, it allows you to enables template/block hints directly in the mail preview window.

See it here : https://github.com/OddBrew/Oddbrew_MailViewer

This is a free extension, so I hope it does not fall under the promotional rules of SO.

1
  • Good stuff - thanks! Works well on 1.9.4. Nov 19, 2021 at 18:12
0

http://www.magentocommerce.com/magento-connect/send-a-test-newsletter-to-an-email.html

Good extension for test newsletter

0

I use a little test script to send the order confirmation email again. This works with Magento 1.9 because it triggers the cronjob to process the email queue. Further it resets the attribute "Email sent" so Magento does not skip sending that email.

include 'app/Mage.php';
Mage::app('default');

$oid = '1234567';

$_order = Mage::getModel('sales/order')->load($oid);    
$_order->setEmailSent('0');
$_order->save();

$_order->sendNewOrderEmail();

// var_dump($_order);

// Trigger email queue
$a = new Mage_Core_Model_Email_Queue();
$a->send();

echo "\nFinished\n";
0

Go to app/code/local/Mage/Core/Model/Email/Template.php file. In send function, around line 441 there is a piece of code

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

it will echo the mail on the browser without actually sending it to the user. One can used this for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.