Magento模块示例发送邮件

427 查看

今天写一个magento模块,主要的功能是实现前台用户利用表单给管理员发送邮件的一个功能,功能很简单,但在这个模块中,可以了解到一些有关magento的一些细节,比如,如何获取系统标签id,如何使用magento默认的提示信息,如何使用smtp服务等等。
新建模块Inchoo
目录结构如下:

Inchoo
 |--_SimpleContact
          |----controllers
                |--IndexController.php
          |----etc
                |--config.xml

首先需要让magento识别写的扩展:
app/etc/modules/Inchoo_SimpleContact.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Inchoo_SimpleContact>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_SimpleContact>
    </modules>    
</config>

其次再在模块的配置文件config.xml中添加:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Inchoo_SimpleContact>
            <version>0.1.0</version>
        </Inchoo_SimpleContact>
    </modules>   
    <frontend>
        <routers>
            <SimpleContact>
                <use>standard</use>
                <args>
                    <module>Inchoo_SimpleContact</module>
                    <frontName>simplecontact</frontName>
                </args>
            </SimpleContact>
        </routers>
    </frontend>    
</config>

在控制中IndexController.php代码如下,在代码中解释器含义:

<?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内容如下:

<div class="box simple_contact">
 
<form id="simple_contact_form" name="simple_contact_form" action="<?php echo $this->getUrl('*/*/sendemail/') ?>" method="post">
 
    <fieldset class="group-select">
        <h4 class="legend">Inchoo_SimpleContact module sample</h4>
        <ul>
        <li>
                <div class="input-box">
                    <label for="name">Gimme your name <span class="required">*</span></label><br />
 
                    <input name="name" id="name" title="Name" value="" class="required-entry input-text" type="text" />
                </div>
 
                <div class="input-box">
                    <label for="email">And your email <span class="required">*</span></label><br />
                    <input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email" type="text" />
                </div>
 
                <div class="clear"></div>
 
                <div class="input-box">
           &nbsp;        <label for="comment">Some comment?</label><br />
 
                    <textarea name="comment" id="comment" title="Comment" class="required-entry input-text" style="height:100px;" cols="50" rows="3"></textarea>
                </div>
                </li>
                </ul>
    </fieldset>
    <div class="button-set">
        <p class="required">* Required Fields</p>
        <button class="form-button" type="submit"><span>Submit</span></button>
 
    </div>
</form>
</div>
    <script type="text/javascript">  
    //< ![CDATA[  
      var myForm= new VarienForm('simple_contact_form', true);  
    //]]>  
    </script>  

页面效果如图: