今天写一个magento模块,主要的功能是实现前台用户利用表单给管理员发送邮件的一个功能,功能很简单,但在这个模块中,可以了解到一些有关magento的一些细节,比如,如何获取系统标签id,如何使用magento默认的提示信息,如何使用smtp服务等等。
新建模块Inchoo
目录结构如下:
Inchoo|--_SimpleContact|----controllers|--IndexController.php|----etc|--config.xml
首先需要让magento识别写的扩展:
app/etc/modules/Inchoo_SimpleContact.xml
其次再在模块的配置文件config.xml中添加:
在控制中IndexController.php代码如下,在代码中解释器含义:
class Inchoo_SimpleContact_IndexController extends Mage_Core_Controller_Front_Action
{public function indexAction(){//Get current layout state$this->loadLayout();//使用mganeto默认的提示信息,需要在控制器添加$this->_initLayoutMessages('core/session');$this->_initLayoutMessages('core/session');//在控制器中创建一个block$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','inchoo.simple_contact',array('template' => 'inchoo/simple_contact.phtml'));$this->getLayout()->getBlock('content')->append($block);//$this->getLayout()->getBlock('right')->insert($block, 'catalog.compare.sidebar', true);$this->_initLayoutMessages('core/session');$this->renderLayout();}//发送邮件public function sendemailAction(){$params = $this->getRequest()->getParams();//print_r($params);//这里获取后台设置的smtp的host和port//$my_smtp_host = Mage::getStoreConfig('system/smtp/host');//$my_smtp_port = Mage::getStoreConfig('system/smtp/port');//以下代码,需要提前安装好smtp的一个扩展插件SMTPPro $general_email=Mage::getStoreConfig('trans_email/ident_general/email');$sales_email = Mage::getStoreConfig('trans_email/ident_sales/email');$support_email = Mage::getStoreConfig('trans_email/ident_support/email');//smtppro$smtppro_host = Mage::getStoreConfig('smtppro/general/smtp_host');$smtppro_port = Mage::getStoreConfig('smtppro/general/smtp_port');$smtppro_username = Mage::getStoreConfig('smtppro/general/smtp_username');$smtppro_password = Mage::getStoreConfig('smtppro/general/smtp_password');$email=Mage::getStoreConfig('contacts/email/recipient_email');$config = array('port' => $smtppro_port,'auth' => 'login','username' => $smtppro_username,'password' => $smtppro_password);$transport = new Zend_Mail_Transport_Smtp($smtppro_host, $config);Zend_Mail::setDefaultTransport($transport);$mail = new Zend_Mail('utf-8');print_r($transport);//exit();$mail->setBodyText($params['comment']);
// $mail->setFrom($config['username']);
// $mail->addTo($params['email'], $params['name']);$mail->setFrom($params['email']);$mail->setSubject('test');$mail->addTo($email);try {$mail->send();$this->_redirect('*/*');Mage::getSingleton('core/session')->addSuccess('Your request has been sent!');}catch(Exception $ex) {print_r($ex->getMessage());Mage::getSingleton('core/session')->addError('Unable to send!');$this->_redirect( $this->getBaseUrl());}}}
备注:代码中提到的SMTP Pro插件,可以在Magento Connect上下载源文件,然后安装到自己的项目中即可,安装成功后,可以看到在systerm->Configuration左侧看到ASCHRODER EXTENSIONS/SMTP Pro,点击看到如下界面:
提示:如果安装好后,打开报错,应先清理缓存,然后logout注销,重新登录后台即可。
控制器中有
$my_smtp_host = Mage::getStoreConfig('system/smtp/host');
$my_smtp_port = Mage::getStoreConfig('system/smtp/port');
这里是获取magento自己的input值,如图所示:
同理可得port的input值。
前台页面代码如下:
路径如下:
app/design/frontend/rwd/default/template/inchoo/simple_contact.phtml
simple_contact.phtml内容如下:
* Required Fields
页面效果如图: