在magento自定义模块中包含邮件功能

时间:2013-10-04 06:18:27

标签: magento magento-1.7 email-integration

我有我的自定义模块客户反馈/查询表,其中客户可以询问与产品相关的查询,或者他们能够提供与商店相关的反馈。在管理方面,我列出了所有反馈管理网格。

现在我想集成邮件功能,就像我点击特定反馈编辑部分时,将有单独的邮件正文部分,我将在其中输入回复,点击发送按钮,邮件发送给特定客户哪个邮件ID已被已存在于该特定编辑部分。

以下是 AdminHtml控制器文件

的代码
<?php
class Foo_Bar_Adminhtml_BazController extends Mage_Adminhtml_Controller_Action
  {
public function indexAction()
{
    // Let's call our initAction method which will set some basic params for each action

    $this->_initAction()
    ->renderLayout();
}

public function newAction()
{
    // We just forward the new action to a blank edit form
    $this->_forward('edit');
}

public function editAction()
{
    $this->_initAction();

    // Get id if available
    $id  = $this->getRequest()->getParam('id');
    $model = Mage::getModel('foo_bar/baz');

    if ($id) {
        // Load record
        $model->load($id);

        // Check if record is loaded
        if (!$model->getId()) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('This baz no longer exists.'));
            $this->_redirect('*/*/');

            return;
        }
    }

    $this->_title($model->getId() ? $model->getName() : $this->__('New Baz'));

    $data = Mage::getSingleton('adminhtml/session')->getBazData(true);
    if (!empty($data)) {
        $model->setData($data);
    }

    Mage::register('foo_bar', $model);

    $this->_initAction()
    ->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
    ->_addContent($this->getLayout()->createBlock('foo_bar/adminhtml_baz_edit')->setData('action', $this->getUrl('*/*/save')))
    ->renderLayout();
}

public function saveAction()
{
    if ($postData = $this->getRequest()->getPost()) {

        $model = Mage::getSingleton('foo_bar/baz');
        $model->setData($postData);

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The baz has been saved.'));
            $this->_redirect('*/*/');

            return;
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this baz.'));
        }

        Mage::getSingleton('adminhtml/session')->setBazData($postData);
        $this->_redirectReferer();
    }
}
public function deleteAction()
{
    // check if we know what should be deleted
    $itemId = $this->getRequest()->getParam('id');
    if ($itemId) {
        try {
            // init model and delete
            /** @var $model Magentostudy_News_Model_Item */
            $model = Mage::getModel('foo_bar/baz');
            $model->load($itemId);
            if (!$model->getId()) {
                Mage::throwException(Mage::helper('foo_bar')->__('Unable to find a Baz.'));
            }
            $model->delete();

            // display success message
            $this->_getSession()->addSuccess(
                    Mage::helper('foo_bar')->__('The Baz has been deleted.')
            );
        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Exception $e) {
            $this->_getSession()->addException($e,
                    Mage::helper('foo_bar')->__('An error occurred while deleting the baz.')
            );
        }
    }

    // go to grid
    $this->_redirect('*/*/');
}
public function messageAction()
{
    $data = Mage::getModel('foo_bar/baz')->load($this->getRequest()->getParam('id'));
    echo $data->getContent();
}

/**
 * Initialize action
 *
 * Here, we set the breadcrumbs and the active menu
 *
 * @return Mage_Adminhtml_Controller_Action
 */
protected function _initAction()
{
    $this->loadLayout()
    // Make the active menu match the menu config nodes (without 'children' inbetween)
    ->_setActiveMenu('sales/foo_bar_baz')
    ->_title($this->__('Sales'))->_title($this->__('Baz'))
    ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
    ->_addBreadcrumb($this->__('Baz'), $this->__('Baz'));

    return $this;
}

/**
 * Check currently called action by permissions for current user
 *
 * @return bool
 */
protected function _isAllowed()
{
    return Mage::getSingleton('admin/session')->isAllowed('sales/foo_bar_baz');
}

}

我想要一些钩子,我可以从中发送邮件给特定的客户。 这是我的管理网格部分的图像

admin grid image

1 个答案:

答案 0 :(得分:0)

最简单的方法是创建一个新的交易邮件,并将主题设置为占位符,并为主体设置相同的内容。

这是交易邮件功能:

/**
 * Send transactional email to recipient
 *
 * @param   int $templateId
 * @param   string|array $sender sneder informatio, can be declared as part of config path
 * @param   string $email recipient email
 * @param   string $name recipient name
 * @param   array $vars varianles which can be used in template
 * @param   int|null $storeId
 * @return  Mage_Core_Model_Email_Template
 */
public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

所以首先要做的是,在系统 - &gt;交易邮件下创建一个新的交易邮件。现在只需填写一些随机的东西。然后 转到您要发送电子邮件的位置并添加

Mage::getModel('core/email_template')
    ->sendTransactional(
        {the transactional email id we just created}, 
        $sender, 
        $recepientEmail, 
        $recepientName,   
        array(
            'subject' => '{your subject}',
            'body' => '{you body}'
        )
    );

{your subject}{your body}替换为您输入的相应字段。

完成后再回到您的交易电子邮件模板,并用以下内容替换我们的随机内容: 在主题字段中输入{{var subject}} 和交易邮件的内容字段中的{{var body}}

我没试过这个,但它应该有用。

希望有所帮助