Magento – Magento 1.9 – I want to create the own controller in order to implement multiple contact form sent to specific email address

controllersmagento-1.9module

I'm trying to make 2 multiple contact forms in the Magento web site.
And the 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 controls the new one. For instance, contents from the original form could be sent to aaa@gmail.com assigned at the Magento admin page and contents from a 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 forms in the Magento admin page.

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

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

\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!

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">
  1. This is my config file in local module

app\code\local\Kbethos\Contacts\etc\config.xml

<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>

Best Answer

If you look at the code, the line

const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';

Defines the path of the configuration element. Here you need to put your configuration field path where you add your email address for second contact form like,

const XML_PATH_EMAIL_RECIPIENT = 'CONFIG_SECTION/CONFIG_GROUP/CONFIG_EMAIL_FIELD';

First you need to update your controller code in file app\code\local\Kbethos\ContactToSales\controllers\IndexController.php
Change

class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action

To

class Kbethos_ContactToSales _IndexController extends Mage_Core_Controller_Front_Action

Now in your second form, update action section with

<form action="<?php echo $this->getUrl("contactssales/index/post"); ?>" id="contactForm" method="post" class="scaffold-form">
Related Topic