Magento custom emails

20 Comments 8th JUN 2009 | Posted by Branko Ajzele in Magento

Magento custom emails

Ever wanted to “just send the email” using the built in email features in Magento? Ever hit the wall trying to do something with Magento? OK, I know the answer to the other one, just had to ask :) . Anyhow, sending the emails with Magento turned out to be a process of “just” a few hours of tracing Magento code.

I cant wait for smart comments like, “few hours, huh its so easy…”. Yea, thats the beauty of Magento… few hours of bashing your head against the wall while you are sipping the 4th cup of coffee until the solution hits you. Interesting do, just when you get the hang of it, Magento gets you this “have you tried this” attitude :) .

What am I talking about? Scenario: I want to create email template named activecodeline_custom_email1.html, I want to pass few variables to it during runtime, I want to send emails programmaticaly. I dont want to create 56 lines of xml file just to call one template.

Here is how.

...
/*
 * Loads the html file named 'custom_email_template1.html' from
 * app/locale/en_US/template/email/activecodeline_custom_email1.html
 */
$emailTemplate  = Mage::getModel('core/email_template')
						->loadDefault('custom_email_template1');									

//Create an array of variables to assign to template
$emailTemplateVariables = array();
$emailTemplateVariables['myvar1'] = 'Branko';
$emailTemplateVariables['myvar2'] = 'Ajzele';
$emailTemplateVariables['myvar3'] = 'ActiveCodeline';

/**
 * The best part :)
 * Opens the activecodeline_custom_email1.html, throws in the variable array
 * and returns the 'parsed' content that you can use as body of email
 */
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

/*
 * Or you can send the email directly,
 * note getProcessedTemplate is called inside send()
 */
$emailTemplate->send('john@someemail.com','John Doe', $emailTemplateVariables);
...

And here we go again, nothing without xml files :) -In order for above piece of code to work, you need to add an entry to your config.xml file like shown below.

...
<global>
	<template>
		<email>
			<custom_email_template1 module="SampleModule1">
				<label>ActiveCodeline custom email module</label>
				<file>activecodeline_custom_email1.html</file>
				<type>html</type>
			</custom_email_template1>
		</email>
	</template>
</global>
...

And lets not forget the email template itself, app/locale/en_US/template/email/activecodeline_custom_email1.html.

<!--@subject ActiveCodeline custom email module @-->

<div>
<h1>ActiveCodeline custom email example by Branko Ajzele</h1>
<p>Hi there {{var myvar1}} {{var myvar2}} from {{var myvar3}}. This is just some example template to test custom email module.</p>
</div>

Hope this was helpful. Cheers.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

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

There are 20 comments (Add Yours +)

  • Thank you for this tuto, but don’t you miss a

    $emailTemplate->setSenderName(‘my name’);
    $emailTemplate->setSenderEmail(‘my@email.com’);

    before to send?

  • Kim Delmote Says

    François Gueguen is right, it does need the send name and sender email.
    I would like to know though if there is a way to get those details from magento

  • Hi, thanks very much for your trick.
    I want to send an email from Order View Page in admin panel. I tried to use your trick, but it did not work. I am confused that which config.xml I should change. Please help me.

  • the config.xml of your module from the etc. folder

  • Thanks for the tip. Just what I was looking for! Cheers

  • westlifelove Says

    ActiveCodeline custom email module
    activecodeline_custom_email1.html
    html

    xml?????

  • westlifelove Says

    $emailTemplate->send(‘john@someemail.com’,'John Doe’, $emailTemplateVariables);

    send() config?

  • where does the custom_email_template1.html file goes?

  • How do you make a new field required?

  • Nice article, thank you!

    How do you pass the locale information with this technique?

  • Thanks Branko!

  • i am using default template but the problem is i get mail but comments are not showing in that mail,
    how can i resolve the issue
    all details are mailing to me but comments are not in that mail
    hope u understood the problem i am having with mailing

  • Nice tutorial, thank you!
    Can we use it as usual in Magento, I mean can we rewrite it from the administration panel for the different languages?

  • As François Gueguen said before, you must use :

    $emailTemplate->setSenderName(‘NAME’);
    $emailTemplate->setSenderEmail(‘EMAIL@DOMAIN.com’);
    $emailTemplate->setTemplateSubject(‘STATUS CHANGED’);

    Because the function isValidForSend is called when executing the send() function.

  • Great work Buddy.
    But I want to attach one pdf with this mail.:)

    How to do that ?
    Please help.

  • haltabush Says

    Hi Atoz,
    Maybe you can try something like


    $at = $mail-&gt;createAttachment($pdf-&gt;render());

    ?

    You should have a look there :
    http://www.magentocommerce.com/boards/viewthread/10762/P45/
    That’s a bit old, but that’s exactly your problem.

  • It is silly to wait for “smart comments like, “few hours, huh its so easy…”” :) In the modern times we use internet and if we do not know something just ask Google :)

    This introduction is great clounade

  • Haii haltabush,
    Thanks a lot for your support. It worked great . Thanks Buddy…….

  • H,
    I’ve tried to add a new Email Template file (activecodeline_custom_email1.html) to the folder “/app/locale/en_US/template/email” and use it according to the instructions here, but after I did that the New Email Template page in the admin area stoped working… It looks like the page start to load the templates to the
    Load default template- Template dropdown and have an error while doing that so the page stop there (I’m adding a print screen).

    Iv’e tried to delete the “activecodeline_custom_email1.html” file from “/app/locale/en_US/template/email” and clean the cache but it didn’t fixed the problem.

    How can I fix this?

    Buy the way – I’m using Magento ver. 1.4.0.1

  • I’ll really appreciate some help

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009