cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 1.9 - I want to create my own controller in order to implement multiple contact form sent to

Magento 1.9 - I want to create my own controller in order to implement multiple contact form sent to

I'm trying to make 2 multiple contact forms in Magento web site. And original one will be inherited from the original core contact module but when it comes to the new one i made, it needs another controller which literally control the new one. For instance, contents from original form could be sent to aaa@gmail.com assigned at magento admin page and contents from new contact form could be sent to bbb@gmail, means would be specified in magento back-end code.

 

 

The final goal is that each form could be sent to another email address. you know, unless i install the extension about contact form from the 3rd party, i can't afford to handle the multiple form in Magento admin page.

 

  1. So i made the module in local pool. Then, i copy the indexcontroller.php but i have no idea how to state the email addr and where do i have to mention the specific email address.

Could you share your opinion? These are my controller and form action code.

 

Spoiler
\app\code\local\Kbethos\Contacts\controllers\IndexController.php
class Kbethos_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{

const XML_PATH_EMAIL_RECIPIENT  = 'contacts/email/recipient_email';
const XML_PATH_EMAIL_SENDER     = 'contacts/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE   = 'contacts/email/email_template';
const XML_PATH_ENABLED          = 'contacts/contacts/enabled';

public function preDispatch()
{
    parent::preDispatch();

    if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
        $this->norouteAction();
    }
}

public function indexAction()
{
    $this->loadLayout();
    $this->getLayout()->getBlock('contactForm')
        ->setFormAction( Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure())) );

    $this->_initLayoutMessages('customer/session');
    $this->_initLayoutMessages('catalog/session');
    $this->renderLayout();
}

public function postAction()
{
    $post = $this->getRequest()->getPost();
    if ( $post ) {
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
            $postObject = new Varien_Object();
            $postObject->setData($post);

            $error = false;

            if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }

            if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }

            if ($error) {
                throw new Exception();
            }
            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)
                );

            if (!$mailTemplate->getSentSuccess()) {
                throw new Exception();
            }

            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
            $this->_redirect('*/*/');

            return;
        } catch (Exception $e) {
            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }

    } else {
        $this->_redirect('*/*/');
    }
}
}
  1. And i want to know how to modify the second form attributes in order to use local module ! It's just copy of the original one!
    Spoiler
    app/design/frontend/rwd/default/template/contacts/form.phtml
    <form action="<?php echo $this->getUrl("contactssales/index/post"); ?>" id="contactForm" method="post" class="scaffold-form"></form>
  2. This is my config file in local module
    Spoiler
    app\code\local\Kbethos\Contacts\etc\config.xml
  3. <config>
    <modules>
        <Kbethos_Contacts>
            <version>1.6.0.0</version>
        </Kbethos_Contacts>
    </modules>
    <frontend>
        <routers>
            <contacts>
                <use>standard</use>
                <args>
                    <module>Kbethos_Contacts</module>
                    <frontName>contactssales</frontName>
                </args>
            </contacts>
        </routers>
        <translate>
            <modules>
                <Mage_Contacts>
                    <files>
                        <default>Mage_Contacts.csv</default>
                    </files>
                </Mage_Contacts>
            </modules>
        </translate>
        <layout>
            <updates>
                <contact>
                    <file>contacts.xml</file>
                </contact>
            </updates>
        </layout>
    </frontend>
    <global>
        <resources>
            <contacts_setup>
                <setup>
                    <module>Kbethos_Contacts</module>
                </setup>
            </contacts_setup>
        </resources>
        <template>
            <email>
                <contacts_email_email_template translate="label" module="contacts">
                    <label>Contact Form</label>
                    <file>contact_form.html</file>
                    <type>text</type>
                </contacts_email_email_template>
            </email>
        </template>
    </global>
    
    <adminhtml>
        <translate>
            <modules>
                <Mage_Contacts>
                    <files>
                        <default>Mage_Contacts.csv</default>
                    </files>
                </Mage_Contacts>
            </modules>
        </translate>
    </adminhtml>
    
    <default>
        <contacts>
            <contacts>
                <enabled>1</enabled>
            </contacts>
            <email>
                <recipient_email>bbb@gmail.com</recipient_email>
                <sender_email_identity>custom2</sender_email_identity>
                <email_template>contacts_email_email_template</email_template>
            </email>
        </contacts>
    </default>
    </config>