在magento自定义模块中包含邮件功能
我有我的自定义模块客户反馈/查询表,客户可以在其中询问与产品相关的查询,也可以提供与商店相关的反馈。在管理端,我列出了管理网格中的所有反馈 现在我想集成邮件功能,比如当我单击特定的反馈编辑部分时,邮件正文将有一个单独的部分,我将在其中输入回复,单击发送按钮,邮件将发送到特定的客户,该客户的邮件Id已经存在于该特定的编辑部分中 下面是我的AdminHtml控制器文件的代码在magento自定义模块中包含邮件功能,magento,magento-1.7,email,Magento,Magento 1.7,Email,我有我的自定义模块客户反馈/查询表,客户可以在其中询问与产品相关的查询,也可以提供与商店相关的反馈。在管理端,我列出了管理网格中的所有反馈 现在我想集成邮件功能,比如当我单击特定的反馈编辑部分时,邮件正文将有一个单独的部分,我将在其中输入回复,单击发送按钮,邮件将发送到特定的客户,该客户的邮件Id已经存在于该特定的编辑部分中 下面是我的AdminHtml控制器文件的代码 <?php class Foo_Bar_Adminhtml_BazController extends Mage_Adm
<?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');
}
最简单的方法是创建一封新的事务性邮件,将主题设置为占位符,并将主体设置为占位符
这是事务性邮件功能:
/**
* 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)
因此,首先要做的是,在系统->事务性邮件下创建一个新的事务性邮件。现在就用一些随机的东西填满它。然后
去任何你想发送电子邮件的地方并添加
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}}
和事务性邮件的die内容字段中的{var body}
我没有试过这个,但它应该能起作用
希望这会有所帮助谢谢你的帮助。但是每次更改主题和正文都不是一个好的做法。我想要一些更动态的解决方案来直接触发邮件。这是动态的,你只需将动态主题作为变量传递给模板,对正文也是如此