Magento themes and extensions

Magento custom emails

Share

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.

01....
02./*
03.* Loads the html file named 'custom_email_template1.html' from
04.* app/locale/en_US/template/email/activecodeline_custom_email1.html
05.*/
06.$emailTemplate  = Mage::getModel('core/email_template')
07.->loadDefault('custom_email_template1');
08.
09.//Create an array of variables to assign to template
10.$emailTemplateVariables = array();
11.$emailTemplateVariables['myvar1'] = 'Branko';
12.$emailTemplateVariables['myvar2'] = 'Ajzele';
13.$emailTemplateVariables['myvar3'] = 'ActiveCodeline';
14.
15./**
16.* The best part <img src="http://<;a href="http://inchoo.net/" class="alinks_links" onclick="javascript:pageTracker._trackPageview('/outbound/article/inchoo.net');" title="inchoo.net" style="padding-right: 13px; background: url("http://asia-connect.com.vn/wp-content/plugins/alinks/images/external.png" ;) no-repeat scroll right center transparent;" rel="nofollow">inchoo.net</a>/wp-includes/images/smilies/icon_smile.gif" alt=":)">
17.* Opens the activecodeline_custom_email1.html, throws in the variable array
18.* and returns the 'parsed' content that you can use as body of email
19.*/
20.$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
21.
22./*
23.* Or you can send the email directly,
24.* note getProcessedTemplate is called inside send()
25.*/
26.$emailTemplate->send('john@someemail.com','John Doe', $emailTemplateVariables);
27....

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.

01....
02.<global>
03.<template>
04.<email>
05.<custom_email_template1 module="SampleModule1">
06.<label>ActiveCodeline custom email module</label>
07.<file>activecodeline_custom_email1.html</file>
08.<type>html</type>
09.</custom_email_template1>
10.</email>
11.</template>
12.</global>
13....

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

1.<!--@subject ActiveCodeline custom email module @-->
2.
3.<div>
4.<h1>ActiveCodeline custom email example by Branko Ajzele</h1>
5.<p>Hi there {{var myvar1}} {{var myvar2}} from {{var myvar3}}. This is just some example template to test custom email module.</p>
6.</div>

Hope this was helpful. Cheers.

Leave a Reply

You must be logged in to post a comment.