[Magento] Create Contact Form with Dynamic Recipient
There are many ways of creating contact forms to email to multiple recipient. I'm going to describe a way that uses Javascript a the frontend to change the target recipient.
Advantage of this method:
Okay enough talking, lets get started.
Step 1: Set up HTML form fields
I'm going to use an example of drop down select to change the email. Of course, this is fairly easy to modify with other forms as well.
Open up app/design/frontend/default/<theme>/template/contacts/contacts.phtml
Insert the following snippets anywhere between the <form>...</form> tag.
Step 2: Setup Javascript for onclick() change.
Step 3: IndexController hack
Now we going to change the email handler (controller) in Magento core.
Open up app/code/core/Mage/Contacts/controllers/IndexController.php
Search for these lines
Add the following codes before those lines.
Replace line
with
We are done!
Its a very simple hack thought. Its good enough for beginners to get started hacking. :)
Advantage of this method:
- Easy to add in new emails in the future.
Disadvantage of this method:
- For those who are particularly concern email privacy, you might not want to use this method. You can modify the email checking to the backend (controller) instead.
Okay enough talking, lets get started.
Step 1: Set up HTML form fields
I'm going to use an example of drop down select to change the email. Of course, this is fairly easy to modify with other forms as well.
Open up app/design/frontend/default/<theme>/template/contacts/contacts.phtml
Insert the following snippets anywhere between the <form>...</form> tag.
<select name="interest" id="interest" title="<?php echo Mage::helper('contacts')->__('Please select department to contact') ?>" value="" class="input-text required-entry" type="text" onchange="changeIntendedRecipient()" > <option value="Department 1">Department 1</option> <option value="Department 2">Department 2</option> <option value="Department 3">Department 3</option> <option value="Department 4">Department 4</option> </select> <input type="hidden" name="intended_recipient_email" id="intended_recipient_email" value="default_email@example.com" /> <input type="hidden" name="intended_recipient_name" id="intended_recipient_name" value="Default name" />
Step 2: Setup Javascript for onclick() change.
<script type="text/javascript"> function changeIntendedRecipient() { switch(document.getElementById("interest").selectedIndex) { case 0: document.getElementById("intended_recipient_email").value = "email1@example.com"; document.getElementById("intended_recipient_name").value = "Recipient 1"; break; case 1: document.getElementById("intended_recipient_email").value = "email2@example.com"; document.getElementById("intended_recipient_name").value = "Recipient 2"; break; case 2: document.getElementById("intended_recipient_email").value = "email3@example.com"; document.getElementById("intended_recipient_name").value = "Recipient 3"; break; case 3: document.getElementById("intended_recipient_email").value = "email4@example.com"; document.getElementById("intended_recipient_name").value = "Recipient 4"; break; default: } } </script>
Step 3: IndexController hack
Now we going to change the email handler (controller) in Magento core.
Open up app/code/core/Mage/Contacts/controllers/IndexController.php
Search for these lines
$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) );
Add the following codes before those lines.
if (htmlentities(trim($post['intended_recipient_email']))) { $newrecipient = array('email' => htmlentities(trim($post['intended_recipient_email'])), 'name' => htmlentitiestrim($post['intended_recipient_name']))); } else { $newrecipient = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT); }
Replace line
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
with
$newrecipient,
We are done!
Its a very simple hack thought. Its good enough for beginners to get started hacking. :)
Hi Grant, thats for pointing out the error, I left out the hidden fields in Step 1, thats why the Javascript couldn't work.
ReplyDeleteYou can try again by adding in the hidden fields in step 1 :)
tried that and im getting:
ReplyDeleteFatal error: Call to undefined function htmlentitiestrim() /code/core/Mage/Contacts/controllers/IndexController.php on line 93
ie, $newrecipient = array('email' => htmlentities(trim($post['intended_recipient_email'])), 'name' => htmlentitiestrim($post['intended_recipient_name']));
Hey Bing, i still can't get this working, if you can figure out the error i will defiantly donate to your paypal!
ReplyDeleteThanks for the code mate, i fixed the error, and thanks for your help!
ReplyDeleteHi there! Was away for holiday over the long weekend. Glad that you've fixed the error! ;)
ReplyDeleteThanks for the pointing out the error.I have intrested html code.Java script is very goog.This script type is very wonderfull.
ReplyDeleteThank you buddy. Did some error in coding. Your code works perfect. Did this moderation in Magento product: http://www.apptha.com/magento/ajax-cart-pro
ReplyDeleteMagento’s user-friendly control interface features highly effective marketing, seo and catalog-management resources to give suppliers the power to create sites that are designed to their unique business needs.
ReplyDeleteMagento Cloud Hosting
Magento is an open source web application which is better known for building powerful eCommerce based websites. It provides the agility of an open source technology by playing nice with third-party extensions and modules, and offers users unprecedented control over their online storefronts. There are a lot of extensions or modules made by the Magento community or third-party coders, and are readily available to make your e-commerce solution features rich and get your online sales booming.
ReplyDeleteMagento Hosting I Magento Cloud Hosting
Hey great Coding...... Thank you for sharing This video Amazing.
ReplyDeleteWeb Development India
Hi! Ive had a problem when sending:
ReplyDeleteFatal error: Call to undefined function htmlentitiestrim() in /usr/home/pekebebe.es/web/app/code/core/Mage/Contacts/controllers/IndexController.php on line 96
I can load the list but its something wrong when sending
I dont know what to try for fixing it