38

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?

WackGet
  • 2,667
  • 3
  • 36
  • 50
  • Could you hardcode it to send the email to you? – Wing Lian Dec 11 '12 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 '12 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) – MagePal Extensions Dec 11 '12 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 '12 at 09:54
  • What about all the other email notifications? – ermannob Apr 30 '14 at 07:14

11 Answers11

39

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!

WackGet
  • 2,667
  • 3
  • 36
  • 50
  • 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. – Hervé Guétin Apr 03 '14 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);
Brideo
  • 163
  • 5
chickahoona
  • 1,914
  • 14
  • 23
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
MutantTurkey
  • 536
  • 5
  • 14
  • 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. – Luke A. Leber Sep 25 '16 at 03: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 '18 at 15:18
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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 '12 at 16:21
  • That's true. Keep resending the same order to finetune your transactional email. – Tom van den Heuvel Dec 13 '12 at 03:58
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.

Matthias Zeis
  • 1,734
  • 18
  • 25
  • 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 '14 at 13:25
  • 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. – joaogdesigner Apr 29 '17 at 02: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). – Matthias Zeis Apr 29 '17 at 22:13
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

jissereitsma
  • 122
  • 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 '13 at 23:37
  • 2
    and this buddy is just proposing a solution, i dont think any SO rule is violated here... – Zaffar Saffee Sep 16 '14 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 '15 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.

Iggy
  • 2,014
  • 25
  • 21
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.

OddBrew
  • 498
  • 3
  • 15
0

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

Good extension for test newsletter

Alex
  • 769
  • 1
  • 11
  • 18
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";
Stefan
  • 8,456
  • 3
  • 29
  • 38
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.

Rajeev Singh
  • 143
  • 10