Quite often it becomes necessary to send an alert or a letter with any additional information to the store user. I think that many are aware of the need / usefulness of sending letters from the site. Here the
Magento developers did their best to provide us with a rather powerful mail handling mechanism.
What's so special about it?
So some may ask: “Why not use the
mail () function? After all, it is precisely designed for such purposes. ” Of course, you can, especially if someone can not live without it, God himself ordered mail-it. What gives us the magento-vsky mechanism for sending letters? The first and, in my opinion, the most important advantage is the use of templates. Templates are ordinary html-files with small additions (but more on that below), i.e. the template can have any design, and the maker-ups will no longer spit, disassembling your php-code and trying to embody the designer's idea. Also in the templates, you can add blocks of any of the available modules (just do not forget to initialize them), which allows you to increase the speed of development.
Where to begin?
So, the
sendTransactional () method of the model
Mage_Core_Model_Email_Template deals with sending letters
.Mage::getModel( 'core/email_template' )
->setDesignConfig(array( 'area' => 'frontend' , 'store' =>$storeId))
->sendTransactional(
$templateId,
$sender,
$recepientEmail,
$recepientName,
$templateData);
* This source code was highlighted with Source Code Highlighter .
')
$ storeId - store identifier (yes,
Magento can pick up several stores, read about it
here )
$ templateId -
template identifier, it can be either a string identifier, which is taken from the xml-config, or integer, which is taken from the database
With integer identifiers, everything is simple, you can view them in the admin
> System> Transactional Emails section and, if necessary, add your own template. String identifiers are in the configurations of the modules in the
global / template / email section. For example,
< global >
< template >
< email >
< customer_create_account_email_template translate ="label" module ="customer" >
< label > New account </ label >
< file > account_new.html </ file >
< type > html </ type >
</ customer_create_account_email_template >
</ email >
</ template >
</ global >
* This source code was highlighted with Source Code Highlighter .
customer_create_account_email_template is our identifier. As you can see, the
customer_create_account_email_template element has three child elements — it's
label ,
file ,
type .
- label - template name
- file - the path to the template file relative to the current locale (by default, this is app / locale / en_US / template / email)
- type - template type (either text or html)
$ sender - the sender of the letter, can be represented as an associative array, for example:
$sender = array('email' => 'sender@example.com', 'name' => 'Sender Name');
Or it is taken from the Magento config, most often from the
System> Configuretion> Store Email Addresses admin section.
$sender = Mage::getStoreConfig('customer/create_account/email_identity');
In this example, the path leads to the
General contact configuration of the admin
> System> Configuretion> Store Email Addresses section.
$ recepientEmail - email recipient
$ recepientName - recipient name
$ templateData is an associative array containing the data to be transferred to the template. For example:
$templateData = array('name' => ' ', 'link_url' => 'http://google.ru', 'items_collection' => $items, 'phone_number' => '11122233');
Letter Templates
As I wrote above, the letter template is an html-file with small additions. So, the template begins with a header, which indicates the subject of the letter like this:
Further, any html-code can follow, i.e. after the header we put the body of the letter.
…
How to use the data that we put in
$ templateData ? Very simple. You must insert the following
{{var <key_name>}} into the template. For example,
< a href ="{{var link_url}}" > {{var name}} </ a >
* This source code was highlighted with Source Code Highlighter .
At the exit we get:
< a href =” http: // google . ru ” > < a >
* This source code was highlighted with Source Code Highlighter .
Another convenient feature is the use of layouts in templates. Those. You can describe the layout in your module, and use it later in the letter. For example, if you add to the mymodule.xml configuration of your module’s layout
< mymodule_email_someitems >
< block type ="mymodule/email_items" name ="items" template ="email/mymodule/items.phtml" />
</ mymodule_email_someitems >
* This source code was highlighted with Source Code Highlighter .
then adding the following construction to the template will allow to display the block (s) inside the layout:
{{layout handle="mymodule_email_someitems" items=$items_collection}}
If the layout needs any data, it can be passed in the additional attributes of the
{{layout}} construct, in this case, this is the items parameter.
And the last thing I would like to talk about letter templates is conditional expressions. They are described using the design:
{{depend condition}}
,
{{/depend}}
If the
condition value is true, the contents of the depend block will be displayed. For example:
{{depend phone_number}}
{{var phone_number}} – .
{{/depend}}
This is how mailing is implemented. This method allows you to use all the advantages of this great CMS.