Send email using email template in magento manually
Posted by navaneeth on Dec 5, 2013 in Magento | 4 Comments
Using the default mail sending and template functionality of magento we can also send our custom email to the customers using the created email templates in the magento admin
so to recaps we are discussing about the send email using email template in magento manually
Here is the code snippets to do so
$customer = Mage::getSingleton('customer/session')->getCustomer(); //suppose the customer whose data to be fetched is 2 if(!empty($customer)) { $mailTemplate = Mage::getModel('core/email_template'); /* @var $mailTemplate Mage_Core_Model_Email_Template */ $translate = Mage::getSingleton('core/translate'); $templateId = 2; //template for sending customer data $template_collection = $mailTemplate->load($templateId); $template_data = $template_collection->getData(); if(!empty($template_data)) { $templateId = $template_data['template_id']; $mailSubject = $template_data['template_subject']; //fetch sender data from Adminend > System > Configuration > Store Email Addresses > General Contact $from_email = Mage::getStoreConfig('trans_email/ident_general/email'); //fetch sender email $from_name = Mage::getStoreConfig('trans_email/ident_general/name'); //fetch sender name $sender = array('name' => $from_name, 'email' => $from_email); $vars = array('variable'=>'value' ); //for replacing the variables in email with data /*This is optional*/ $storeId = Mage::app()->getStore()->getId(); $model = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject); $email = $customer->getEmail(); // $email = 'navaneeth008@gmail.com'; $name = $customer->getName(); $model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId); if (!$mailTemplate->getSentSuccess()) { throw new Exception(); } $translate->setTranslateInline(true); } }
we have specified and
$templateId = 2; //template for sending customer data
This is the template id we created in the magento admin for our purpose
and
$vars = array('variable'=>'value' ); //for replacing the variables in email with data
This is the array of variables used to replace the content in the email ie if the email template has the value like
{variable}
its replaced by the “value” field we are passing through the array
To be concluded we discussed here about how we can send email using email template in magento manually with ease.
Ping me if you have any doubts
4 Comments on “Send email using email template in magento manually”
Deepak
December 22, 2015Where to write this code
navaneeth
December 24, 2015Code can be written in a controller or where ever you like to sent the mail
Kam
October 5, 2016How can I call this from a button? (on click it will send a transactional email)
navaneeth
October 10, 2016Yes have to make it inside a controller function