Couple of days back I had to work with mail sending in magento e-commerce. Here I am giving a complete example of how to do that:-
Magento sends mails with it’s model “core/email_template”. Here is an example code for your understanding:-
01 |
/** |
02 |
* $templateId can be set to numeric or string type value. |
03 |
* You can use Id of transactional emails (found in |
04 |
* "System->Trasactional Emails"). But better practice is |
05 |
* to create a config for this and use xml path to fetch |
06 |
* email template info (whatever from file/db). |
07 |
*/ |
08 |
const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template' ; |
09 |
$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH); |
10 |
11 |
$mailSubject = 'HI this is a test mail.' ; |
12 |
13 |
/** |
14 |
* $sender can be of type string or array. You can set identity of |
15 |
* diffrent Store emails (like 'support', 'sales', etc.) found |
16 |
* in "System->Configuration->General->Store Email Addresses" |
17 |
*/ |
18 |
$sender = Array( 'name' => 'S. M. Asad Rahman' , |
19 |
'email' => 'asad.dk.bd@gmail.com' ); |
20 |
21 |
/** |
22 |
* In case of multiple recipient use array here. |
23 |
*/ |
24 |
$email = 'smasadrahman@yahoo.com' ; |
25 |
26 |
/** |
27 |
* If $name = null, then magento will parse the email id |
28 |
* and use the base part as name. |
29 |
*/ |
30 |
$name = 'Asad Rahman' ; |
31 |
32 |
$vars = Array(); |
33 |
/* An example how you can pass magento objects and normal variables*/ |
34 |
/* |
35 |
$vars = Array('customer'=>$customer, |
36 |
'address' =>$address, |
37 |
'varification_data'=>'fake data for example');*/ |
38 |
39 |
/*This is optional*/ |
40 |
$storeId = Mage::app()->getStore()->getId(); |
41 |
42 |
$translate = Mage::getSingleton( 'core/translate' ); |
43 |
Mage::getModel( 'core/email_template' ) |
44 |
->setTemplateSubject( $mailSubject ) |
45 |
->sendTransactional( $templateId , $sender , $email , $name , $vars , $storeId ); |
46 |
$translate ->setTranslateInline(true); |
Now lets put email template information in config.xml of the corresponding module.
01 |
<? xml version = "1.0" ?> |
02 |
< config > |
03 |
<!-- Other config infos |
04 |
goes here |
05 |
. |
06 |
. |
07 |
.--> |
08 |
< global > |
09 |
<!-- Other config infos |
10 |
goes here |
11 |
. |
12 |
. |
13 |
.--> |
14 |
< template > |
15 |
< email > |
16 |
< customer_testemail_email_template translate = "label" module = "mymodulename" > |
17 |
< label >Test email sending</ label > |
18 |
< file >test_email_template.html</ file > |
19 |
< type >html</ type > |
20 |
</ customer_testemail_email_template > |
21 |
</ email > |
22 |
</ template > |
23 |
</ global > |
24 |
</ config > |
This config xml says there is a email template in “app/locale/en_US/template/email” there is a email template named “test_email_template.html”, so lets create the template file:-
01 |
< div style = "font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;" > |
02 |
< table cellspacing = "0" cellpadding = "0" border = "0" width = "98%" style = "margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;" > |
03 |
< tr > |
04 |
< td align = "center" valign = "top" > |
05 |
<!-- [ header starts here] --> |
06 |
< table cellspacing = "0" cellpadding = "0" border = "0" width = "650" > |
07 |
< tr > |
08 |
< td valign = "top" > |
09 |
< p > |
10 |
< a href = "{{store url=" "}}" style = "color:#1E7EC8;" >< img src = "{{skin url=" images/logo_email.gif" _area = 'frontend' }}" alt = "Magento" border = "0" /></ a > |
11 |
</ p > |
12 |
</ td > |
13 |
</ tr > |
14 |
</ table > |
15 |
16 |
<!-- [ middle starts here] --> |
17 |
< table cellspacing = "0" cellpadding = "0" border = "0" width = "650" > |
18 |
< tr > |
19 |
< td valign = "top" > |
20 |
< p > |
21 |
< strong >Dear {{var customer.name}}</ strong >,< br /> |
22 |
This is a test mail.:-) |
23 |
</ p > |
24 |
</ td > |
25 |
</ tr > |
26 |
</ table > |
27 |
28 |
</ td > |
29 |
</ tr > |
30 |
</ table > |
31 |
</ div > |
In config.xml we set values for “customer_testemail_email_template”, but in code we use Mage::getStoreConfig(“customer/testemail_email/template”) to set $templateId. So we need to create a relation in between two. To do so we will create a installer to insert a data in db config.
01 |
<?php |
02 |
03 |
$installer = $this ; |
04 |
/* @var $installer Mage_Core_Model_Resource_Setup */ |
05 |
06 |
$configValuesMap = array ( |
07 |
'customer/testemail_email/template' => |
08 |
'customer_testemail_email_template' , |
09 |
); |
10 |
11 |
foreach ( $configValuesMap as $configPath => $configValue ) { |
12 |
$installer ->setConfigData( $configPath , $configValue ); |
13 |
} |
That’s it. Now this code snippet is good enough to send a customized mail. If you want the facility to set email template from admin panel, use the following system.xml in your module.
01 |
<? xml version = "1.0" ?> |
02 |
< config > |
03 |
< sections > |
04 |
< customer > |
05 |
< groups > |
06 |
< testemail translate = "label" > |
07 |
< label >Apply Non-Profit Account Options</ label > |
08 |
< sort_order >100</ sort_order > |
09 |
< show_in_default >1</ show_in_default > |
10 |
< show_in_website >1</ show_in_website > |
11 |
< show_in_store >1</ show_in_store > |
12 |
< fields > |
13 |
< email_template translate = "label" > |
14 |
< label >Email Template</ label > |
15 |
< frontend_type >select</ frontend_type > |
16 |
< source_model >adminhtml/system_config_source_email_template</ source_model > |
17 |
< sort_order >3</ sort_order > |
18 |
< show_in_default >1</ show_in_default > |
19 |
< show_in_website >1</ show_in_website > |
20 |
< show_in_store >1</ show_in_store > |
21 |
</ email_template > |
22 |
</ fields > |
23 |
</ testemail > |
24 |
</ groups > |
25 |
</ customer > |
26 |
</ sections > |
27 |
</ config > |
That’s it !!! I hope this post will help you sending emails from magento. Please leave comments .