Sunday, 15 January 2012

Magento - code for sending a custom transactional mail

Many a times developer need to code for sending a transactional mail which is not the clone of the custom mail in Magento.
Here is the code for this functionality

$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);

$email = Mage::getModel('core/email_template');
/* @var $email Mage_Core_Model_Email_Template */        

$template =  Mage::getModel('core/email_template') ->loadByCode('YOUR NEW TRANSACTIONAL MAIL NAME')->getTemplateId();
$recipient = array(
  'email' => $_user->getEmail(),
  'name'  => $_user->getName()
);


$sender  = array(
  'name' => Mage::getStoreConfig('trans_email/ident_support/name', Mage::app()->getStore()->getId()),
  'email' => Mage::getStoreConfig('trans_email/ident_support/email', Mage::app()->getStore()->getId())
);

$email->setDesignConfig(array('area'=>'frontend', 'store'=>Mage::app()->getStore()->getId()))
    ->sendTransactional(
      $template,
      $sender,
      $recipient['email'],
      $recipient['name'],
      array(
        'name'    => $_user->getName(),
        'email'   => $_refuser->getEmail(),
        'point'   => Mage::getStoreConfig('rewardpoints/registration/referral_reg_points', Mage::app()->getStore()->getId()),
        'store_name' => Mage::getModel('core/store')->load(Mage::app()->getStore()->getCode())->getName()
      )
    );

$translate->setTranslateInline(true);
return $email->getSentSuccess();
In transactional mail as the content of the template you can use the custom variable as bellow:

Name: {{var name}}

Email: {{var email}}

Point: {{var point}}

Store Name: {{var store_name}}

1 comment :