Showing posts with label Send email. Show all posts
Showing posts with label Send email. Show all posts

Friday, 20 January 2012

Send Custom Email In Magento

 Below is the example how to send custom email in magento.


/**
   * Send email Magento
   */
  function sendEmail() {
    if ($this->getRequest()->getPost()) {
      /**
       * $templateId can be set to numeric or string type value.
       * You can use Id of transactional emails (found in
       * "System->Trasactional Emails").
       */
      $collection = Mage::getResourceSingleton('core/email_template_collection')
              ->addFieldToFilter('template_code', array('any_templae_code'));
      foreach ($collection as $value) {
        $templateId = $value->getTemplateId();
      }
      $name = 'Ajit';
      $email = 'ajit@test.com';
      $comment = 'seems working send email';

      $mailSubject = 'Subject Any;

      /**
       * $sender can be of type string or array. You can set identity of
       * diffrent Store emails (like 'support', 'sales', etc.) found
       * in "System->Configuration->General->Store Email Addresses"
       */
      $sender = array('name' => $name,
          'email' => $email);


      /**
       * For multiple recipient use array here.
       */
      $email = Mage::getStoreConfig('trans_email/ident_custom1/email');

    
      $name = 'Admin';

      $vars = Array('comment' => $comment);
     

      /*  optional */
      $storeId = Mage::app()->getStore()->getId();

      $translate = Mage::getSingleton('core/translate');
      Mage::getModel('core/email_template')
              ->setTemplateSubject($mailSubject)
              ->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
      $translate->setTranslateInline(true);
      $this->_getSession()->addSuccess('Email Sent);
      $this->_redirect('any_url');
    } else {
      $this->_getSession()->addError('Invalid access');
      $this->_redirect('any_url');
    }
  }


Happy coding ;)