Magento E-commerce Platform Q/ A

Email Notification of Magento 'Contact Us' Form Submitter

The feature notifies the user who submits 'Contact Us' form on site (the notification will be delivered to the indicated by the user email address).

See the image:

248

The notification message example:

249

To enable the feature:

1. We will extend Contacts settings with the ability to Enable/ Disable notification and the ability to choose the template of the notification to send to the user.

Add file system.xml into the module folder:

app/code/local/Ikantam/TM/etc/system.xml

<config>
    <sections>
        <contacts>
            <groups>
                 <sender_notification translate="label">
                    <label>Sender Notification</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>60</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                         <enabled translate="label">
                            <label>Enable Sender Notification</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <backend_model>contacts/system_config_backend_links</backend_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enabled>
                        <email_template translate="label">
                            <label>Email Template (Sender Notification)</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_email_template</source_model>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </email_template>
                    </fields>
                </sender_notification>
            </groups>
        </contacts>
    </sections>
</config>

 

As a result, in System -> Configuration ->Contacts tab additional options group 'Sender Notification' will appear. See the image:

250

 

2. The next step is to add an html-template of the letter.

/app/locale/en_US/template/email/contact_sender_notification.html

<!--@subject Subject  @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var logo_url":"Email Logo Image Url",
"var logo_alt":"Email Logo Image Alt",
"htmlescape var=$data.name" : "Email sender contact form"
}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
    <tr>
        <td align="center" valign="top" style="padding:20px 0 20px 0">
            <!-- [ header starts here] -->
            <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                <tr>
                    <td valign="top">
                        <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0"/></a>
                    </td>
                </tr>
                <!-- [ middle starts here] -->
                <tr>
                    <td valign="top">
                        <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;">Dear {{htmlescape var=$data.name}},</h1>
                        <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;">Some text</p>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you again, <strong>{{var store.getFrontendName()}}</strong></p></center></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</div>
</body>

 

After adding the code (see above) we will declare the template in the configuration file and will set the template as default in Email Template (Sender Notification)

app/code/local/Ikantam/TM/etc/config.xml

<config>
    …
     <global>
         ...
        <template>
            <email>
               <contacts_sender_notification_email_template translate="label" module="tm">
                   <label>Contact Sender Notification</label>
                   <file>contact_sender_notification.html</file>
                   <type>html</type>
               </contacts_sender_notification_email_template>
            </email>
        </template>
        ...
    </global>
     ...
    <default>
        <contacts>
            <sender_notification>
                <email_template>contacts_sender_notification_email_template</email_template>
            </sender_notification>
        </contacts>
    </default>
...
</config>

 

As a result, in the configuration of the Email Template (Sender Notification) we will see the newly added template. See the image:

251

 

3. Now we will rewrite the controller in the config file: Mage_Contacts_IndexController

app/code/local/Ikantam/TM/etc/config.xml

<config>
    ...
    <global>
        ...
        <rewrite>
            <ikantam_tm_contacts_index>
                <from><![CDATA[#^/contacts/index/#]]></from>
                <to>/tm/contacts_index/</to>
            </ikantam_tm_contacts_index>
        </rewrite>
        ...
     </global>
     …
</config>

 

And will replace app/code/local/Ikantam/TM/controllers/Contacts/IndexController.php controller with:

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

class Ikantam_TM_Contacts_IndexController  extends Mage_Contacts_IndexController
{
    const XML_PATH_SENDER_NOTIFICATION_ENABLED        = 'contacts/sender_notification/enabled';
    const XML_PATH_SENDER_NOTIFICATION_EMAIL_TEMPLATE = 'contacts/sender_notification/email_template';

    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();
                }


                /* send sender notification */
                if (Mage::getStoreConfigFlag(self::XML_PATH_SENDER_NOTIFICATION_ENABLED)) {
                    $customerMailTemplate = Mage::getModel('core/email_template');
                    /* @var $mailTemplate Mage_Core_Model_Email_Template */
                    $customerMailTemplate->setDesignConfig(array('area' => 'frontend'))
                        ->setReplyTo(self::XML_PATH_EMAIL_RECIPIENT)
                        ->sendTransactional(
                            Mage::getStoreConfig(self::XML_PATH_SENDER_NOTIFICATION_EMAIL_TEMPLATE),
                            Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                            $post['email'],
                            null,
                            array('data' => $postObject)
                        );

                    if (!$customerMailTemplate->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('*/*/');
        }
    }

}

 

That's it.