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 ;)

2 comments:

  1. Hi Ajit,

    Are you working on magento?

    Can you share me live site url?

    Thanks,
    Vallabh

    ReplyDelete
  2. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Magento 2 Hosting Company List

    ReplyDelete