Creating automatic emails for custom events in Magento

by Yaroslav Rogoza

November 24, 2011 Comments (68)

Magento is a relatively intelligent system. It implements an Event/Observer pattern for end users to hook into. You can catch different events and process them with your logic after. For example, if you want to perform an action after a user was logged in, you can use customer_login event, for an action implementation after the event.
Also, you can use Event/Observer technology for sending custom emails after an action has taken place.
For example, let’s say we need to send a custom email when a customer’s order has been canceled.
Let see what we can do here.

Step1:

First, you should make your own module. Here is a brief example. If you need more information – feel free reading my previous blogposts.

Create a file /app/etc/modules/Namespace_Custmail.xml with following content:

You can create the html file in a subfolder with your own name, but you should describe all of this in the template email section config file.
Note that words the ‘Namespace’ and ‘Custmail’ will need to be replaced with ‘YourCompanyName’ and ‘YourModuleName’.

Now, let’s go back to explanations.
We have an observer class Namespace_Custmail_Model_Observer with two methods.
Method invoicedStatusChange calls when an event sales_order_save_commit_after occurred (it was described in our config file in the observer method section). It checks the current order status and if status = ‘Canceled’ the method calls _sendStatusMail.
Method _sendStatusMail generates an email with the necessary data.

We have to fill 4 required parameters:

$emailTemplate->loadDefault(..template file..) – Here ..template file.. is our template’s identifier. We have described it in the config file

<custom_order_tpl>
...
</custom_order_tpl>

$emailTemplate->setTemplateSubject(..subject..) – This is the email subject, make sure you fill it in.

As for the $emailTemplateVariables array, it contains data that we want to store in our template. As you can see from step 3, there are constructions like ‘{{var username}}‘. So, it’s nothing more than value in the $emailTemplateVariables array with index ‘username’. In other words, if you have the value $emailTemplateVariables[‘my_mothers_name’], you can get this value in the template file with construction {{var my_mothers_name}}. Note: if you want to use your variables in templates, you should add your var as the argument when you call ‘send’ method. $emailTemplate->send($order->getCustomerEmail(), $order->getStoreName(), $emailTemplateVariables) has $emailTemplateVariables (our values set) as the 3rd argument.

You can also send emails in other formats. For example, if you want to send your custom letter in plain text, you should use

<type>text</type>

in the config.xml when describing the template section.

That’s it! Now customers will receive our custom email when their orders get canceled. You can also handle any other magento event with observers and send custom emails to anyone (customer/admin/girlfriend etc..).
Happy coding!

Subscribe to our blog

Interestedin Atwix?

Smart Brands Choose Us.

From Mark Cuban and Sir Richard Branson backed startups, to Inc 500 US Fastest Growing Companies and Global Brands choose us to deliver and support unparalleled eСommerce experience for their customers.