3

I want to make a functionality that based on what I select in contact form (I've add a select box in the contact form).

For example if I select a department from the select box I want that the email goes to that department and not to the central email for contact form.

How can I do this?

1 Answer 1

2

You have to override postAction():

app/etc/modules/SeStro_Contacts.xml

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

app/code/local/SeStro/Contacts/etc/config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <SeStro_Contacts>
        <modules>
            <version>0.1.0</version>
        </modules>
    </SeStro_Contacts>
    <frontend>
        <routers>
            <contacts>
                <args>
                    <modules>
                        <SeStro_Contacts before="Mage_Contacts">SeStro_Contacts</SeStro_Contacts>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>
</config>

app/code/local/SeStro/Contacts/controllers/IndexController.php

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts').DS.'IndexController.php';

class SeStro_Contacts_IndexController
    extends Mage_Contacts_IndexController
{
    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();
                }

                $selectedEmail = $post['name_of_select_list']; // you have to change key of $post array
                switch($selectedEmail) {
                    case 1:
                        $email = Mage::getStoreConfig('trans_email_ident_general_email');
                        break;
                    case 2:
                        $email = Mage::getStoreConfig('trans_email_ident_sales_email');
                        break;
                    case 3:
                        $email = Mage::getStoreConfig('trans_email_ident_support_email');
                        break;
                    case 4:
                        $email = Mage::getStoreConfig('trans_email_ident_custom1_email');
                        break;
                    case 5:
                        $email = Mage::getStoreConfig('trans_email_ident_custom2_email');
                        break;
                    default:
                        $email = Mage::getStoreConfig('trans_email_ident_general_email');
                }

                $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),
                        $email,
                        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('*/*/');
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.