【问题标题】:Include mail functionality in magento custom module在 magento 自定义模块中包含邮件功能
【发布时间】:2013-10-04 06:18:26
【问题描述】:

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

现在我想集成邮件功能,比如当我点击特定的反馈编辑部分时,邮件正文会有单独的部分,我将在其中输入回复,点击发送按钮,邮件会发送给邮件 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');
}

}

我想要一些挂钩,我可以通过这些挂钩向特定客户发送邮件。 这是我的管理网格部分的图像

【问题讨论】:

    标签: magento magento-1.7 email


    【解决方案1】:

    最简单的方法是创建一个新的事务性邮件并将主题设置为占位符,正文也是如此。

    这是交易邮件功能:

    /**
     * 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)
    

    所以首先要做的是,在 System->Transactional Mails 下创建一个新的事务邮件。现在只需用一些随机的东西填充它。然后 转到您要发送电子邮件的地方并添加

    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}} 在事务邮件的内容字段中

    我没有尝试过,但它应该可以工作。

    希望对你有帮助

    【讨论】:

    • 感谢您的帮助。但是每次更改主题和正文时都不是一个好习惯,我想要一些更动态的解决方案来直接发送邮件。
    • 那是动态的,您只需将动态主题作为变量传递给模板,正文也是如此
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    相关资源
    最近更新 更多